I have created a listview that sits inside a larger activity (the list view fills half the screen, with inputs/buttons above it). This listview uses a custom adapter to draw the row views.
Inside the row view is a button, which when clicked/tapped i want the activity to handle, and not the adapter. However i don’t know how inside the adapter class, i can tell it to use the activity. Since it’s OOP, im assuming i have to pass some sort of reference to the activity when setting up the adapter (rather than hardcoding the parent activity into the adapter).
I had a simlier issue with sharing a dataset between the activity and adapter (i wanted the adapter to use an arraylist from the activity), however i couldn’t solve that one either so i ended up passing the arraylist through as a duplicate into the adapter. So i would hope working out the click listener, will also mean i can get rid of that duplicate data having to be created?
All the code is pretty simple, but here’s a rough outline:
Setting the list adapter & declaring dataset (this is the activity dataset, not the adapter)
numbersListAdapter = new NumberListAdapter(this);
numbersList.setAdapter(numbersListAdapter);
this.selectedContacts = new HashMap<Long, HashMap<String, String>>();
Adding entry into activity data set and adding to the adapter dataset
HashMap<String, String> tempa = new HashMap<String,String>();
tempa.put("name", name);
tempa.put("number", number);
this.selectedContacts.put(contactID, tempa);
this.numbersListAdapter.addEntry(contactID, tempa);
The adapters add entry
public void addEntry(Long id, HashMap<String, String> entry) {
entry.put("contactID", id.toString());
this.selectedNumbers.add(entry);
this.notifyDataSetChanged();
}
The adapters constructor
public NumberListAdapter(Context context) {
selectedNumbers = new ArrayList<HashMap<String, String>>();
mInflater = LayoutInflater.from(context);
}
Please note: this is my first attempt at this stuff. I’ve never done android, java and very, very little OO programming. So i already know the code is most likely inefficient and quite terrible. But i’ve got to learn somehow 🙂
EDIT
Ok so i realised ive been a little silly and i just needed to use the context passed into the adapter to reference the parent activity. However i still ain’t getting it. Heres the code:
The constructor for the adapter
numbersListAdapter = new NumberListAdapter(this);
The variable declaration and constructor method
public class NumberListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<HashMap<String, String>> selectedNumbers;
private Context parentActivity;
public NumberListAdapter(Context context) {
parentActivity = (Context) context;
selectedNumbers = new ArrayList<HashMap<String, String>>();
mInflater = LayoutInflater.from(context);
}
The listener
Button theBtn = (Button)rowView.findViewById(R.id.actionRowBtn);
theBtn.setOnClickListener(this.parentActivity);
I get two messages from eclipse, the first occurs when i comment out the listener and i get
The value of the field NumberListAdapter.parentActivity is not used
Once i add the listener in i get
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (Context)
Obviously im doing something wrong. Probably quite a silly mistake again too
If you need to get callback when row of the listview is clicked you can use
But if you need a button inside each row to be clicked you will have to override the
getView function of your adapter and then set the onclicklistener of the button individually
Edit:
Typecast the Context to Activity