繁体   English   中英

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

[英]Retrieving data from SQLite and displaying in 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);
    }
 }  

您的表“联系人”似乎没有您尝试阅读的确切结构。

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!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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