android中Sqlite的ListView [英] ListView From Sqlite in android

查看:32
本文介绍了android中Sqlite的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我搜索整个网络时,来自 Sqlite 的 ListView 仍然存在问题.在搜索了这么多之后,我正在 android hive 示例上尝试我的项目 此处链接.因此,在 Database Handler 类中,他们给出了一个函数,即 List getAllContacts() 以获取 List 格式的所有 sqlite 数据.我已经在我的项目中实现了这个,我在 ViewContact.class 中使用了上面的函数.问题 是我不明白如何使用这种类型的方法或任何其他方法获取 ListView 中的所有数据.

查看我的代码(ViewContact.class):

public class ViewContact extends Activity {DatabaseHandler helper = new DatabaseHandler(this);字符串 a;@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.row);ListView listContent = (ListView)findViewById(R.id.listView1);}公共列表<联系方式>getAllContacts() {列出<联系方式>contactList = new ArrayList();//选择所有查询String selectQuery = "SELECT * FROM 联系人";SQLiteDatabase db = helper.getWritableDatabase();Cursor cursor = db.rawQuery(selectQuery, null);//遍历所有行并添加到列表如果 (cursor.moveToFirst()) {做 {联系人contact = new Contact();contact.setID(Integer.parseInt(cursor.getString(0)));contact.setName(cursor.getString(1));contact.setPhoneNumber(cursor.getString(2));//添加联系人到列表contactList.add(contact);} while (cursor.moveToNext());}//返回联系人列表返回联系人列表;}}

<小时>

在@GrIsHu 回答之后看到输出是:

解决方案

尝试将数据绑定到listview如下:

<块引用>

列表<联系方式>contact = new ArrayList();联系人=getAllContacts();ArrayAdapter 适配器 = 新的 ArrayAdapter(this, android.R.layout.simple_list_item_1, 联系人);listContent.setAdapter(适配器);

As I searched whole net still in problem with ListView From Sqlite. After searching so much i am trying my project on android hive example Link here. So in this in Database Handler class they have given that a function i.e List getAllContacts() to get all sqlite data in List format. I have implemented this in my project my using above function in ViewContact.class. The PROBLEM is that I am not understanding how to get all data in ListView by using this type of method or by any other method.

See my code (ViewContact.class) :

public class ViewContact extends Activity {
DatabaseHandler helper = new DatabaseHandler(this);
String a;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.row);
    ListView listContent = (ListView)findViewById(R.id.listView1);
    }
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM contacts";
    SQLiteDatabase db = helper.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;}}

EDIT:


See After @GrIsHu answer the output is :

解决方案

Try to bind the data into the listview as below:

List<Contact> contact = new ArrayList<Contact>(); 
contact=getAllContacts();     
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contact); 
 listContent.setAdapter(adapter);

这篇关于android中Sqlite的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆