Hae stackoverflow gurus,
I'm looking for an assistance. i have researched all over the internet but in vain. I would like to add all values fetched to listview. I have managed to insert values to the database and managed to fetch all of them however, i'm unable to add them to listview. The function below works perfect and returns the data as list.
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;
}
Also, the function below works perfect and prints out all the retrieved values using log
}
private void showRecords() {
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
//lv.setAdapter(new dataAdapter(this, cn.getID(), cn.getName(), cn.getPhoneNumber()));
String log = "Id: " + cn.getID() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to log
//Log.d("Name: ", log);
Toast.makeText(getApplicationContext(),log, Toast.LENGTH_SHORT).show();
}
}
Now, i have created a listview for which is working however, i'm unable to add data to the listview as shown.
private void showRecords() {
List<Contact> contacts = db.getAllContacts();
for(int i=0; i < contacts.size();i++){
lv.setAdapter(new dataAdapter(this, cn.getID(),cn.getName(), cn.getPhoneNumber()));
}
}
My question is, how can i populate List<Contact> contacts = db.getAllContacts(); to the listview lv?
This is the entire dataAdapter class
public class dataAdapter extends BaseAdapter {
Context context;
String [] name;
String [] phone;
int [] dataId;
private static LayoutInflater inflater=null;
public dataAdapter(Context context, int[] dataId, String[] name, String[] phone){
this.context=context;
this.dataId=dataId;
this.name=name;
this.phone=phone;
inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return dataId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public class Holder{
TextView nameV;
TextView phoneV;
}
public View getView(final int position, View convertView, ViewGroup parent){
Holder holder=new Holder();
View rowView;
rowView=inflater.inflate(R.layout.thelist, null);
holder.nameV=(TextView) rowView.findViewById(R.id.txt1);
holder.phoneV=(TextView) rowView.findViewById(R.id.txt2);
holder.nameV.setText(name[position]);
holder.phoneV.setText(phone[position]);
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,name[position],Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
}
showRecordsmethod is wrong. You don't loop through the records and set the adapter for each one. You should create a single instance of an Adapter backed by yourList<Contact>and then set it once