How to call main activity's function from custom ArrayAdapter?

GrilledCheese picture GrilledCheese · Feb 12, 2013 · Viewed 19.9k times · Source

I've looked at a lot of similar questions and can't seem to get anything to work. I have a main class with a function like this that edits shows a dialog box then edits a List when a button is pressed.

public class EditPlayers extends SherlockFragmentActivity {

    listPlayerNames.setAdapter(new EditPlayerAdapter(ctx,
                R.layout.score_row_edit_player, listScoreEdit));

public void deletePlayer(final int position) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
            EditPlayers.this);

    // Setting Dialog Title
    alertDialog.setTitle("Delete Player");

    // Setting Dialog Message
    alertDialog.setMessage("Are you sure?");

    // Setting Delete Button
    alertDialog.setPositiveButton("Delete",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    listScoreEdit.remove(position);
                    updateListView();
                }
            });

    // Setting Cancel Button
    alertDialog.setNeutralButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

    // Showing Alert Message
    alertDialog.show();
}

}

How do I access that function from the getView() in the adapter? Here's the XML for the row

<TextView
    android:id="@+id/nameEdit"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:layout_weight="70"
    android:text="Name"
    android:textColor="#666666"
    android:textSize="22sp"
    android:textStyle="bold" >
    </TextView>

<Button
    android:id="@+id/deletePlayer"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="30"
    android:text="Delete"
    android:focusable="false" />

Here's the getView()

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    convertView = (LinearLayout) inflater.inflate(resource, null);    
    Score score = getItem(position);
    TextView txtName = (TextView) convertView.findViewById(R.id.nameEdit);
    txtName.setText(score.getName());
    Button b = (Button)convertView.findViewById(R.id.deletePlayer);
    b.setTag(position);
    b.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 //call function here
             }
     });
    return convertView;
}

I'm totally lost at this point so any help would be appreciated. Thanks!

Answer

James McCracken picture James McCracken · Feb 12, 2013

I would recommend providing an interface back to your activity that lets it know when that button is pressed. I would not recommend calling an activity's method from an ArrayAdapter. It is too tightly coupled.

Try something like this:

Your Activity

public class EditPlayers extends SherlockFragmentActivity implements EditPlayerAdapterCallback {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        EditPlayerAdapter adapter = new EditPlayerAdapter(this,
                R.layout.score_row_edit_player, listScoreEdit);
        adapter.setCallback(this);
        listPlayerNames.setAdapter(adapter);

    }

    private void deletePlayer(final int position) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                EditPlayers.this);

        // Setting Dialog Title
        alertDialog.setTitle("Delete Player");

        // Setting Dialog Message
        alertDialog.setMessage("Are you sure?");

        // Setting Delete Button
        alertDialog.setPositiveButton("Delete",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        listScoreEdit.remove(position);
                        updateListView();
                    }
                });

        // Setting Cancel Button
        alertDialog.setNeutralButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        // Showing Alert Message
        alertDialog.show();
    }

    @Override
    public void deletePressed(int position) {

        deletePlayer(position);
    }

}

Adapter:

public class EditPlayerAdapter extends ArrayAdapter {

    private EditPlayerAdapterCallback callback;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = (LinearLayout) inflater.inflate(resource, null);    
        Score score = getItem(position);
        TextView txtName = (TextView) convertView.findViewById(R.id.nameEdit);
        txtName.setText(score.getName());
        Button b = (Button)convertView.findViewById(R.id.deletePlayer);
        b.setTag(position);
        b.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {

                     if(callback != null) {

                        callback.deletePressed(position);
                     }
                 }
         });
        return convertView;
    }

    public void setCallback(EditPlayerAdapterCallback callback){

        this.callback = callback;
    }


    public interface EditPlayerAdapterCallback {

        public void deletePressed(int position);
    }
}