繁体   English   中英

从sqlite数据库中检索多个值,并将其填充在android的列表视图中

[英]retrieve multiple values from sqlite database and populate it in listview in android

Hae stackoverflow专家,

我正在寻找协助。 我已经在整个互联网上进行了研究,但徒劳无功。 我想将所有获取的值添加到listview。 我已经设法将值插入数据库并设法获取所有它们,但是,我无法将它们添加到listview中。 下面的函数可以完美工作,并以列表形式返回数据。

 public List<Contact> getAllContacts() {
        List<Contact> contactList = new ArrayList<Contact>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setName(cursor.getString(1));
                contact.setPhoneNumber(cursor.getString(2));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }

另外,下面的函数可以完美工作,并使用log打印所有检索到的值

}


 private void showRecords() {

        List<Contact> contacts = db.getAllContacts();

        for (Contact cn : contacts) {

            //lv.setAdapter(new dataAdapter(this, cn.getID(), cn.getName(), cn.getPhoneNumber()));
            String log = "Id: " + cn.getID() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
            // Writing Contacts to log
            //Log.d("Name: ", log);
            Toast.makeText(getApplicationContext(),log, Toast.LENGTH_SHORT).show();
        }
    }

现在,我创建了一个正在运行的列表视图,但是,我无法如图所示将数据添加到列表视图。

 private void showRecords() {

            List<Contact> contacts = db.getAllContacts();

           for(int i=0; i < contacts.size();i++){
                lv.setAdapter(new dataAdapter(this, cn.getID(),cn.getName(), cn.getPhoneNumber()));
          }         
    }

我的问题是,如何填充List<Contact> contacts = db.getAllContacts(); 到列表视图lv?

这是整个dataAdapter类

public class dataAdapter extends BaseAdapter {

    Context context;
    String [] name;
    String [] phone;
    int [] dataId;
    private static LayoutInflater inflater=null;
    public dataAdapter(Context context, int[] dataId, String[] name, String[] phone){

        this.context=context;
        this.dataId=dataId;
        this.name=name;
        this.phone=phone;

        inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return dataId.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public  class  Holder{
        TextView nameV;
        TextView phoneV;
    }

    public  View getView(final  int position, View convertView, ViewGroup parent){
        Holder holder=new Holder();
        View rowView;
        rowView=inflater.inflate(R.layout.thelist, null);

        holder.nameV=(TextView) rowView.findViewById(R.id.txt1);
        holder.phoneV=(TextView) rowView.findViewById(R.id.txt2);


        holder.nameV.setText(name[position]);
        holder.phoneV.setText(phone[position]);

        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context,name[position],Toast.LENGTH_SHORT).show();

            }
        });
        return  rowView;
    }
}

不要为每个新项目设置适配器,而是仅将联系人列表传递给列表视图适配器一次。 如果要添加新项,请将添加添加到现有适配器。

你可以这样

private void showRecords() {
   List<Contact> contacts = db.getAllContacts();
   lv.setAdapter(new dataAdapter(this, contacts));       
}

或者,您可以按照上面注释中提供的教程进行操作,以更好地理解。

编辑1而不是获取List<Contact> contacts = db.getAllContacts()的每个字段的数组,而是获取一个列表,该List<Contact> contacts = db.getAllContacts()来自List<Contact> contacts = db.getAllContacts()

.....
.....
List<Contact> mContacts;

DataAdapter(Context context,List<Contact> contact) {
    this.context=context;
    this.mContacts=contacts;  
    .....
    .....  
}

您的getCount方法应为

public int getCount() {
    return mContact.size();
}

并且您的getView()方法应该是

public  View getView(final  int position, View convertView, ViewGroup parent) {
    Contact contact = mContact.get(position);
    ....
    ....
    holder.nameV.setText(contact.getName());
    holder.phoneV.setText(contact.getPhone());
    ....
    ....
    return view;
}

并且如果您添加到此现有适配器,请添加一个这样的自定义方法

public void add(Contact contact) {
    if(mContact != null) {
        mContact.add(contact);
        // This will refresh the list.
        notifyDataSetChanged();
    }
}

我仍然建议您仔细阅读上面链接中的教程,您的适配器类需要进行大量更改才能更好地实现。

暂无
暂无

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

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