将SQLite中的记录显示到ListView中

时间:2014-05-07 11:50:21

标签: android sqlite

虽然这里已经多次询问过这个问题,但我找不到合适的答案来适应我的代码。我意识到它可能很小,但我似乎无法找到问题。

这是我的RunDatabase类。

     // Getting All Contacts
public List<RecordLists> getAllContacts() {
    List<RecordLists> contactList = new ArrayList<RecordLists>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_NAME;
    CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
    SQLiteDatabase db = helper.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            RecordLists contact = new RecordLists();
            contact.setId(Integer.parseInt(cursor.getString(0)));
            contact.setUserName(cursor.getString(1));
            contact.setFirstName(cursor.getString(2));
            contact.setLastName(cursor.getString(3));
            contact.setPassword(cursor.getString(4));

            String name = cursor.getString(1) +"\n"+ cursor.getString(2) + "\n" + cursor.getString(3) + "\n" + cursor.getString(4);
            AllRecords.ArrayofName.add(name);
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

现在我在另一个活动中调用它,这个活动是AllRecords.java,代码是:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

        try {
            RunDatabase db = new RunDatabase(this);

            // Reading all contacts
            Log.d("Reading: ", "Reading all Records..");


            List<RecordLists> contacts = db.getAllContacts();   
            ArrayAdapter<RecordLists> adapter = new ArrayAdapter<RecordLists>(this, R.layout.records, contacts);
            for (RecordLists cn : contacts) 
            {   
            final String log = "Id: "+cn.getId()+ " ,UserName " + cn.getUserName() + " ,FirstName: " + cn.getFirstName() + " ,LastName: " + cn.getLastName() + " ,Password " + cn.getPassword();
                // Writing Contacts to log      
            Log.d("Name: ", log);

                    RecordList = (ListView) findViewById(R.id.list_View);
                    RecordList.setEmptyView(findViewById(android.R.id.empty));
                    RecordList.setAdapter(adapter);

1 个答案:

答案 0 :(得分:1)

游标适配器是填充列表视图的最佳选择,返回的数据来自数据库。下面是根据您的要求更改游标适配器的示例代码。

public class adapter extends CursorAdapter{

        public adapter (Context context, Cursor c) {
            super(context, c);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void bindView(View view, Context context, Cursor c) {
            // TODO Auto-generated method stub


            //here write code for populating list view

        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflate=LayoutInflater.from(context);
            View v=inflate.inflate( R.layout.records, parent,false);
            bindView(v, context, cursor);
            return v;
        }


    }

现在通过在需要的地方创建对象来调用适配器。