SQLite表不显示活动数据

时间:2015-04-12 14:52:21

标签: android sqlite

我已成功使用SQlite创建了一个数据库表。但是,当我尝试使用自定义适配器在行中显示数据时,活动为空,因为它不会显示我保存到其中的数据。

这是我从扩展SQLiteOpenHelper的DBHelper类中获取数据的方式



public ArrayList<ContactListItems> getAllContacts() {
        ArrayList<ContactListItems> contactList = new ArrayList<>();
        hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from contacts", null);
            if (res.moveToFirst()) {
                do {
            ContactListItems contactListItems = new ContactListItems();

            contactListItems.setTitle(res.getString(res
                    .getColumnIndex("title")));
            contactListItems.setAmount(res.getString(res
                    .getColumnIndex("amount")));
            contactListItems.setDescription(res.getString(res
                    .getColumnIndex("description")));
            res.moveToNext();
                } while (res.moveToNext());
            }

        return contactList;
    }
&#13;
&#13;
&#13;

MyBasket 活动中,这是我使用 ContactListAdapter 显示数据的方式:

&#13;
&#13;
 private void showTable() {
        ArrayList<ContactListItems> contactList = myDb.getAllContacts();

        ContactListAdapter contactListAdapter = new ContactListAdapter(
                MyBasket.this, contactList);
        //adding it to the list view.
obj = (ListView) findViewById(R.id.listView1);
        obj.setAdapter(contactListAdapter);
    }
&#13;
&#13;
&#13;

ContactListItems:

&#13;
&#13;
public class ContactListItems {
    String title;
    String amount;
    String description;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
&#13;
&#13;
&#13;

ContactListAdapter:

&#13;
&#13;
public class ContactListAdapter extends BaseAdapter {

    Context context;
    ArrayList<ContactListItems> contactList;

    public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {

        this.context = context;
        contactList = list;
    }

    @Override
    public int getCount() {

        return contactList.size();
    }

    @Override
    public Object getItem(int position) {

        return contactList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ContactListItems contactListItems = contactList.get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.contact_list_row, null);

        }
        TextView tvSlNo = (TextView) convertView.findViewById(R.id.tv_slno);
        tvSlNo.setText(contactListItems.getTitle());
        TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
        tvName.setText(contactListItems.getAmount());
        TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
        tvPhone.setText(contactListItems.getDescription());

        return convertView;
    }

}
&#13;
&#13;
&#13;

代码运行良好,没有错误,但没有数据显示。我真的没有看到我哪里出错。任何帮助将不胜感激,如果您需要更多代码,请告诉我。

修改 这样才能更好地理解 当我使用这段代码时:

&#13;
&#13;
private void showTable() {
        ArrayList<String> array_list = myDb.getAllContacts();

        ArrayAdapter arrayAdapter;
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array_list);

        //adding it to the list view.
        obj = (ListView) findViewById(R.id.listView1);
        obj.setAdapter(arrayAdapter);
    }
&#13;
&#13;
&#13;

而且:

&#13;
&#13;
public ArrayList<String> getAllContacts() {
        ArrayList<String> array_list = new ArrayList<>();
        hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from contacts", null);
        res.moveToFirst();
        while (res.isAfterLast() == false) {
            array_list.add("Product: " + res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
            array_list.add("Amount: " + res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
            array_list.add("Description: " + res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
            System.out.print("database data:" + res.getString(res.getColumnIndex(CONTACTS_COLUMN_TITLE)));
            System.out.print("database data:" + res.getString(res.getColumnIndex(CONTACTS_COLUMN_AMOUNT)));
            System.out.print("database data:" + res.getString(res.getColumnIndex(CONTACTS_COLUMN_DESC)));
            System.out.print("List holds:" + array_list);
            res.moveToNext();
        }
        return array_list;
    }
&#13;
&#13;
&#13;

数据显示正常,但我想自定义使用arraylist的行。

1 个答案:

答案 0 :(得分:2)

问题出在getAllContacts()

你错过了:

 contactList.add( contactListItems) 
if块中的

基本上,您正在使用有效的内存引用和空数据返回contactList对象。