【问题标题】:Can't Get Android ListView Working Properly无法让 Android ListView 正常工作
【发布时间】:2015-01-12 14:38:55
【问题描述】:

我正在尝试为我的应用创建一个ListView,但我得到了以下答案

java.lang.RuntimeException: 你的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView

当我成功使ListView 工作时出现此问题,但在添加或删除数据 mysq sqlite 数据库后它不会更新。

我的ListView 代码;

MessageRepo repo = new MessageRepo(this);
msgsList =  repo.getMessageList();
lv = getListView();
ArrayAdapter<HashMap<String, String>> arrayAdapter = new ArrayAdapter<HashMap<String, String>>(this,android.R.layout.simple_list_item_1, msgsList);
setListAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();//my second problem

getMessageList() 代码:

public ArrayList<HashMap<String, String>>  getMessageList() {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String selectQuery =  "SELECT  " +
                Message.KEY_ID + "," +
                Message.KEY_message +
                " FROM " + Message.TABLE;

        //Message Message = new Message();
        ArrayList<HashMap<String, String>> MessageList = new ArrayList<HashMap<String, String>>();


        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list

        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> Messagehash = new HashMap<String, String>();
                Messagehash.put("id", cursor.getString(cursor.getColumnIndex(Message.KEY_ID)));
                Messagehash.put("message", cursor.getString(cursor.getColumnIndex(Message.KEY_message)));
                MessageList.add(Messagehash);

            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return MessageList;

    }

列表视图xml代码:

<ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

提前致谢!

【问题讨论】:

    标签: java android sqlite listview android-listview


    【解决方案1】:

    最好的方法是创建一个扩展 ArrayAdapter 的适配器 像这样:

    private class StableArrayAdapter extends ArrayAdapter<String> {
    
        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
    
        public StableArrayAdapter(Context context, int textViewResourceId,
            Cursor cameFromDB) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {
            //Put your cursor logic here
          }
        }
    
        @Override
        public long getItemId(int position) {
          String item = getItem(position);
          return mIdMap.get(item);
        }
    
        @Override
        public boolean hasStableIds() {
          return true;
        }
    
      }
    
    } 
    

    你的 XML 应该是这样的:

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 
    

    您可以使用带有 ListActivity 或 ListFragment 的自定义布局。在这种情况下,片段或活动在提供的布局中搜索具有预定义 android:id 属性设置为 @android:id/list 的 ListView。

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-05
      相关资源
      最近更新 更多