简体   繁体   中英

Retrieving data from SQLite and displaying in Listview

I am trying to display the contents of my mysqlite database into a listview, I am able to get the contents and display them in a textview, but for some reason I can't add the details to an arraylist ? I am not too sure what am doing wrong. I have looked for multiple solutions but none of them seem to work, am getting an error

Android.database.CursorIndexOutOfBoundsExecption: Index requested -1

Here is what I currently have:

OnCreate:

    ArrayAdapter<Contact> currentContactsAdapter = new ContactArrayAdapter();

    ListView lvcontacts = (ListView) findViewById(R.id.lvContacts);

    lvcontacts.setAdapter(currentContactsAdapter);

    tdb = new TestDBOpenHelper(this, "contact.db", null, 1);
    sdb = tdb.getWritableDatabase();

    new MyContacts().execute();

ListView Adapter:

private class ContactArrayAdapter extends ArrayAdapter<Contact>{

    public ContactArrayAdapter(){
        super(MainActivity.this, R.layout.listviewitem, addedContacts);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if(itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.listviewitem, parent, false);
        }

        Contact currentContact = addedContacts.get(position);       

        TextView name = (TextView) itemView.findViewById(R.id.tvNameitem);
        name.setText(currentContact.getName());

        TextView phone = (TextView) itemView.findViewById(R.id.tvPhoneitem);
        phone.setText(currentContact.getPhone());

        TextView email = (TextView) itemView.findViewById(R.id.tvEmailitem);
        email.setText(currentContact.getEmail());

        return itemView;
    }
}

GetContacts:

class MyContacts extends AsyncTask<String, String, String> {

    List<Contact> retrievedContacts = new ArrayList<Contact>();

protected String doInBackground(String... args) {

        String cname;
        String cphone;
        String cemail;

        // name of the table to query
        String table_name = "contact";
        // the columns that we wish to retrieve from the tables
        String[] columns = {"FIRST_NAME", "PHONE", "EMAIL"};
        // where clause of the query. DO NOT WRITE WHERE IN THIS
        String where = null;
        // arguments to provide to the where clause
        String where_args[] = null;
        // group by clause of the query. DO NOT WRITE GROUP BY IN THIS
        String group_by = null;
        // having clause of the query. DO NOT WRITE HAVING IN THIS
        String having = null;
        // order by clause of the query. DO NOT WRITE ORDER BY IN THIS
        String order_by = null;
        // run the query. this will give us a cursor into the database
        // that will enable us to change the table row that we are working with
        Cursor c = sdb.query(table_name, columns, where, where_args, group_by, 
        having, order_by);

        for(int i  = 0; i < c.getCount(); i++) {
            cname  =  c.getString(c.getColumnIndex("FIRST_NAME"));
            cphone =  c.getString(c.getColumnIndex("PHONE"));
            cemail =  c.getString(c.getColumnIndex("EMAIL"));
            c.moveToNext();
            retrievedContacts.add(new Contact(cname,cphone,cemail));
        }

        return null;
    }

//Update Contact list when response from server is received 
@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    for(Contact contact: retrievedContacts)
        addedContacts.add(contact);
    }
 }  

It seems that your table "contact" doesn't have the exact structure you are trying to read.

Android.database.CursorIndexOutOfBoundsExecption: Index requested -1

This means that one of these column names is not part of it.

c.getColumnIndex("FIRST_NAME") 
c.getColumnIndex("PHONE")
c.getColumnIndex("EMAIL")

So one of them return -1 instead of the index because they not exist in the table.

EDIT:

Then the for loop may be faulty. I suggest to use something like:

if (c != null ) {
    if  (c.moveToFirst()) { // Always move at the first item
        do {
            cname  =  c.getString(c.getColumnIndex("FIRST_NAME"));
            cphone =  c.getString(c.getColumnIndex("PHONE"));
            cemail =  c.getString(c.getColumnIndex("EMAIL"));
            retrievedContacts.add(new Contact(cname, cphone, cemail));
        } while (c.moveToNext());
    }
}
c.close(); // always close when done!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM