简体   繁体   English

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

[英]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 ? 我试图将我的mysqlite数据库的内容显示到listview中,我能够获取内容并在textview中显示它们,但由于某种原因我无法将这些细节添加到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 Android.database.CursorIndexOutOfBoundsExecption:请求索引-1

Here is what I currently have: 这是我目前拥有的:

OnCreate: 在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: 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: 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 Android.database.CursorIndexOutOfBoundsExecption:请求索引-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. 因此,其中一个返回-1而不是索引,因为它们不存在于表中。

EDIT: 编辑:

Then the for loop may be faulty. 然后for循环可能有问题。 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!

暂无
暂无

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

相关问题 从SQLite数据库检索数据到listView - Retrieving data from SQLite database into listView 从列表视图检索数据并将其显示在textview / imageview中 - Retrieving data from a listview and displaying it in a textview/imageview Android SQLite从数据库中检索数据并以文本形式显示该数据 - Android SQLite retrieving data from the database and displaying that data in text Android-基于onclick listview从SQLite数据库检索数据 - Android - Retrieving data from SQLite database, based on onclick listview 从 SQLite 数据库中检索数据并将其显示到 ListView -android - Retrieving data from SQLite database and display it into ListView -android 在Android中将SQLite数据库中的数据检索到ListView(具有TextViews)的障碍: - Obstacle for retrieving data from SQLite database into a ListView(having TextViews) in Android: 从Firebase检索数据并将其显示在列表视图中的Android应用程序中 - retrieving and displaying data from Firebase into Android app in a listview 从sqlite数据库中检索数据并通过“OnItemClick”将其显示在另一个类中 - retrieving data from sqlite database and displaying it in another class through “OnItemClick” 通过AsyncTask从SQLite数据库检索数据并显示它 - Retrieving Data from SQLite Database through AsyncTask and Displaying it 使用来自 sqlite 的自定义适配器视图在 Listview 中显示相同的数据 - Same data displaying in Listview using custom adapterview from the sqlite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM