I am doing a project where I want to display a list of contacts names in a ListView. The names are retrieved from the local sqli db. So far I have managed to retrieve the names and display them using the standard ArrayAdapter class.
However for more control I am trying to create my own adapter to allow me to also display control buttons on each row. I am confused about this piece of code:
private void fillData() {
Cursor mContactsCursor = mDbAdapter.getAllContacts();
startManagingCursor(mContactsCursor);
String [] from = new String[] {ContactsDbAdapter.COLUMN_FNAME, ContactsDbAdapter.COLUMN_LNAME};
int [] to = new int [] { R.id.fname, R.id.lname};
//What variables should constructor take?
adapter = new ContactsListAdapter(this, from, to);
data.setAdapter(adapter);
}
Basically I don't know how to pass these values to the constructor or if I should even do that??
String [] from = new String[] {ContactsDbAdapter.COLUMN_FNAME, ContactsDbAdapter.COLUMN_LNAME};
int [] to = new int [] { R.id.fname, R.id.lname};
This is my ContactsListAdapter class:
public class ContactsListAdapter extends ArrayAdapter<Contact> {
private List<Contact> contacts;
private Button deleteBtn;
private Button editBtn;
private TextView name;
public ContactsListAdapter(Context context,List<Contact> contacts) {
super(context, R.layout.contact_row, contacts);
this.contacts = contacts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.contact_row, null);
}
//assign values to the view
final Contact c = this.contacts.get(position);
//add listeners to buttons
deleteBtn = (Button)v.findViewById(R.id.deleteBtn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
});
editBtn = (Button)v.findViewById(R.id.editBtn);
editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Edit", Toast.LENGTH_SHORT).show();
}
});
//insert name into the text view
name = (TextView)v.findViewById(R.id.name);
name.setText(c.getName());
return v;
}
}
The code for this class was taken from an example where I used a custom list adapter which was getting data from a hard coded array so I probably am missing something when it comes to getting data from the db.
Any advice is much appreciated. Many thanks.