Reputation: 5768
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.
Upvotes: 1
Views: 12777
Reputation: 1631
Refer this example,
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
let me know, if you are still confused how to pass values to your Custom ArrayListAdapter...
Please retrieve all the records from your table as below and add it to a arrayList
contactList.add(contact);
snippet of code,
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
new CustomerAdapter (this, resid, contactList); // this is how you need to the Custom Adapter In your custom Adapter
public class CustomAdapter extends ArrayAdapter {
Context context;
ArrayList<Tip> objects;
public CustomAdapter(Context applicationContext, int dovizLayout, ArrayList<Contact> ts) {
super(applicationContext, dovizLayout);
this.context = applicationContext;
this.objects = (ArrayList) ts;
}
Upvotes: 6