Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 526
GrilledCheese Avatar asked Feb 11 '13 23:02

GrilledCheese


People also ask

How do I use ArrayAdapter?

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.

How to connect an adapter with ListView?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.

What is ArrayAdapter and BaseAdapter?

Here is the difference: BaseAdapter is a very generic adapter that allows you to do pretty much whatever you want. However, you have to do a bit more coding yourself to get it working. ArrayAdapter is a more complete implementation that works well for data in arrays or ArrayList s.


2 Answers

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);
    }
}
like image 199
James McCracken Avatar answered Sep 20 '22 21:09

James McCracken


Your EditPlayerAdapter gets a Context passed to it. Activity extends Context

If the Context passed is your EditPlayers and you store a class-scoped reference to that Context in your Adapter, you can then do:

((EditPlayers) yourContextVar).function();

Better yet, make an interface of some sort. It will help clarify and organise your code and it applies the same principle.

like image 45
A--C Avatar answered Sep 21 '22 21:09

A--C