ListView控件从sqlite的android系统 [英] ListView From Sqlite in android

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

问题描述

当我搜索整个网络仍处于问题的ListView从sqlite的。寻找了这么多,我在Android蜂巢例如想我的项目后,在这里链接。因此,在这个数据库中的处理程序类,他们都给予了一个功能,即列表getAllContacts()来获取所有sqlite的数据列表格式。 我有我使用上述ViewContact.class功能在我的项目实现这一点。 在问题是我不理解如何使用这种方法或任何其他方法来获得在ListView中的所有数据。

请参阅我的code(ViewContact.class):

 公共类ViewContact延伸活动{
数据库处理器辅助=新的数据库处理器(本);
字符串;
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.row);
    ListView控件的listContent =(ListView控件)findViewById(R.id.listView1);
    }
公开名单<联系与GT; getAllContacts(){
    名单<联系与GT; contactList =新的ArrayList<联系与GT;();
    //选择所有查询
    字符串selectQuery =SELECT * FROM接触;
    SQLiteDatabase DB = helper.getWritableDatabase();
    光标光标= db.rawQuery(selectQuery,NULL);
    //遍历所有行和添加到列表
    如果(cursor.moveToFirst()){
        做 {
        联系方式联系我们=新联系();
        contact.setID(的Integer.parseInt(cursor.getString(0)));
        contact.setName(cursor.getString(1));
        contact.setPhoneNumber(cursor.getString(2));
        //添加联系人列出
        contactList.add(接触);
    }而(cursor.moveToNext());
} //返回联系人列表
返回contactList;}}
 

编辑:


请参阅@GrIsHu后回答输出是:

解决方案

尝试将数据绑定到如下列表视图:

 名单,其中,联系与GT;触点=新的ArrayList<联系与GT;();
接触= getAllContacts();
ArrayAdapter适配器=新的ArrayAdapter(这一点,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);

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

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