Steve Kamau
Steve Kamau

Reputation: 2785

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.

Edit So as to understand better When i was using this code:

private void showTable() {
        ArrayList<String> array_list = myDb.getAllContacts();

        ArrayAdapter arrayAdapter;
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array_list);

        //adding it to the list view.
        obj = (ListView) findViewById(R.id.listView1);
        obj.setAdapter(arrayAdapter);
    }

And this :

public ArrayList<String> getAllContacts() {
        ArrayList<String> array_list = new ArrayList<>();
        hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from contacts", null);
        res.moveToFirst();
        while (res.isAfterLast() == false) {
            array_list.add("Product: " + res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
            array_list.add("Amount: " + res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
            array_list.add("Description: " + res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
            System.out.print("database data:" + res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
            System.out.print("database data:" + res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
            System.out.print("database data:" + res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
            System.out.print("List holds:" + array_list);
            res.moveToNext();
        }
        return array_list;
    }

The data was being displayed properly but i wanted to customize the rows insted of using the arraylist.

Upvotes: 0

Views: 92

Answers (1)

Karan
Karan

Reputation: 2130

problem is in getAllContacts()

You missed out on :

 contactList.add( contactListItems) 

in the if block.

Basically you are returning contactList object with valid memory reference and empty data.

Upvotes: 2

Related Questions