android片段sqlite如何获取所有数据

时间:2015-02-09 13:47:54

标签: android sqlite

android片段无法在listview中显示sqlite数据,下面是我的代码。我不明白我做错了什么。

public class WindowsFragment扩展了ListFragment {

@SuppressWarnings("null")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /** Creating array adapter to set data in listview */

    ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
    contactList.clear();
    String query = "SELECT * FROM PHONE_CONTACTS ";
    Cursor c1 = SqlHandler.selectQuery(query);
    if (c1 != null && c1.getCount() != 0) {
        if (c1.moveToFirst()) {
            do {
                ContactListItems contactListItems = new ContactListItems();

                contactListItems.setName(c1.getString(c1
                        .getColumnIndex("name")));

            } while (c1.moveToNext());
        }
    }
    c1.close();

    ContactListAdapter contactListAdapter = new ContactListAdapter(
            getActivity(), contactList);


    ListView simple_list_item_multiple_choice = null;
    simple_list_item_multiple_choice.setAdapter(contactListAdapter);

    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onStart() {
    super.onStart();

    /** Setting the multiselect choice mode for the listview */
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

以下是我的列表适配器`公共类ContactListAdapter扩展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 tvName = (TextView) convertView.findViewById(R.id.tv_name);
    tvName.setText(contactListItems.getName());
    TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
    tvPhone.setText(contactListItems.getPhone());

    return convertView;
}

}

我能够成功运行使用活动但不使用片段

2 个答案:

答案 0 :(得分:0)

您没有在contactlist数组中插入值。传递给listviewadapter时,contactList为空。

添加该行将解决您的问题,它会将ContactListItems对象插入Arraylist。

contactList.add(contactListItems);

代码如下:

if (c1 != null && c1.getCount() != 0) {
    if (c1.moveToFirst()) {
        do {
            ContactListItems contactListItems = new ContactListItems();

            contactListItems.setName(c1.getString(c1
                    .getColumnIndex("name")));
              contactList.add(contactListItems);

        } while (c1.moveToNext());
    }
}
c1.close();

同样在你的listview代码中,我没有看到你正在显示值。

答案 1 :(得分:0)

在这里:

if (c1 != null && c1.getCount() != 0) {
    if (c1.moveToFirst()) {
        do {
            ContactListItems contactListItems = new ContactListItems();

            contactListItems.setName(c1.getString(c1
                    .getColumnIndex("name")));

        } while (c1.moveToNext());
    }
}

您不会向联系人列表添加任何内容。

另外,我使用SQLiteDatabase的Query方法代替原始字符串查询:

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) 

来自http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

只需将表和列设置为tablename和new String [] {&#34; name&#34;},将其他所有内容设置为null以从表中选择每个名称。