从SQLite中检索数据并在Listview中显示

时间:2015-03-07 18:26:43

标签: android sqlite listview

我试图将mysqlite数据库的内容显示到listview中, 我能够获取内容并在textview中显示它们,但对于某些人来说 我无法将细节添加到arraylist的原因?我不太清楚自己在做什么 错误。我已经找了多个解决方案,但它们似乎都没有用,我收到了错误

  

Android.database.CursorIndexOutOfBoundsExecption:索引请求-1

以下是我目前的情况:

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适配器:

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);
    }
 }  

1 个答案:

答案 0 :(得分:0)

好像你的桌子&#34;联系&#34;没有你想要阅读的确切结构。

  

Android.database.CursorIndexOutOfBoundsExecption:索引请求-1

这意味着其中一个列名称不属于它。

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

因此,其中一个返回-1而不是索引,因为它们不存在于表中。

编辑:

然后for循环可能有问题。我建议使用类似的东西:

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!