dimanche 12 avril 2015

SQLite table not displaying data on activity

I have successfully created a database table with SQlite.However when i try to use a custom adapter to display the data in rows,the activity is blank,as in it does not display the data i saved into it.


This is how i get data from my data base in DBHelper class that extends SQLiteOpenHelper





public ArrayList<ContactListItems> getAllContacts() {
ArrayList<ContactListItems> contactList = new ArrayList<>();
hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from contacts", null);
if (res.moveToFirst()) {
do {
ContactListItems contactListItems = new ContactListItems();

contactListItems.setTitle(res.getString(res
.getColumnIndex("title")));
contactListItems.setAmount(res.getString(res
.getColumnIndex("amount")));
contactListItems.setDescription(res.getString(res
.getColumnIndex("description")));
res.moveToNext();
} while (res.moveToNext());
}

return contactList;
}



And in MyBasket Activity,this is how i display the data with ContactListAdapter:





private void showTable() {
ArrayList<ContactListItems> contactList = myDb.getAllContacts();

ContactListAdapter contactListAdapter = new ContactListAdapter(
MyBasket.this, contactList);
//adding it to the list view.
obj = (ListView) findViewById(R.id.listView1);
obj.setAdapter(contactListAdapter);
}



ContactListItems:





public class ContactListItems {
String title;
String amount;
String description;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}



ContactListAdapter:





public class ContactListAdapter extends BaseAdapter {

Context context;
ArrayList<ContactListItems> contactList;

public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {

this.context = context;
contactList = list;
}

@Override
public int getCount() {

return contactList.size();
}

@Override
public Object getItem(int position) {

return contactList.get(position);
}

@Override
public long getItemId(int position) {

return position;
}


@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ContactListItems contactListItems = contactList.get(position);

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_list_row, null);

}
TextView tvSlNo = (TextView) convertView.findViewById(R.id.tv_slno);
tvSlNo.setText(contactListItems.getTitle());
TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
tvName.setText(contactListItems.getAmount());
TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
tvPhone.setText(contactListItems.getDescription());

return convertView;
}

}



The code runs fine with no errors but no display of data.I really do not see where i have gone wrong.Any assistance will be appreciated and if you need more code let me know.


Aucun commentaire:

Enregistrer un commentaire