【问题标题】:Implementing simpleadapter using HashMap使用 HashMap 实现 simpleadapter
【发布时间】:2014-01-07 18:15:02
【问题描述】:

我正在从 sqlite 检索数据并使用 simpleadapter 在列表视图中显示它。我对布局没有任何问题。仍然仅供您参考:

list_item.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <!-- Name Label -->

    <TextView
        android:id="@+id/subject"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"
        android:textColor="#421fc4"
        android:textSize="16sp"
        android:textStyle="bold" />

        <TextView
            android:id="@+id/day"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="2dip"
            android:textStyle="bold" />

    <TextView
        android:id="@+id/slot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:textColorHighlight="#782086"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/instructor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:textColor="#782086"
        android:textStyle="bold" />

</LinearLayout>

我在以下代码 sn-p 中的某处遇到问题我尝试使用普通列表视图从 sqlite 检索数据并且它运​​行完美。但是当我使用此代码 sn-p 使用 SimpleAdapter 自定义列表视图时,我只得到最后一行查询结果。最后一行的数据在列表视图中重复 8 次,因为查询结果中有 8 行。

 HashMap<String, String> hashMap = new HashMap<String, String>();
 ArrayList<HashMap<String, String>> Timetablelist;

//c being the cursor    
    if  (c.moveToFirst()) {
           do {
                String subject = c.getString(c.getColumnIndex(dba.KEY_SUBJECT));
                String day = c.getString(c.getColumnIndex(dba.KEY_DAY));
                String slot = c.getString(c.getColumnIndex(dba.KEY_SLOT));
                String instructor = c.getString(c.getColumnIndex(dba.KEY_INSTRUCTOR_NAME));           
                // adding each child node to HashMap key => value
                hashMap.put("Subject", subject);
                hashMap.put("Day", day);
                hashMap.put("Slot", slot);
                hashMap.put("Instructor", instructor);
                Timetablelist.add(hashMap);
                ListAdapter adapter = new SimpleAdapter(
                                this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day",
                                "Slot","Instructor" }, new int[] { R.id.subject,
                                R.id.day, R.id.slot,R.id.instructor });
                setListAdapter(adapter);                 

              }while (c.moveToNext());

                       }  

P.S : 我能够使用普通的 listview 检索数据。所以数据库和其他布局工作正常。

【问题讨论】:

  • 将 setListAdapter 移出 do while
  • 同时移动适配器实例化:ListAdapter adapter = new SimpleAdapter(
  • 将 sn-p 移到 do while 循环下方但仍然相同。最后一行在列表视图中重复 8 次。

标签: android android-listview hashmap simpleadapter


【解决方案1】:

将下面的内容从 do While 中移出

 ListAdapter adapter = new SimpleAdapter(
                            this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day",
                            "Slot","Instructor" }, new int[] { R.id.subject,
                            R.id.day, R.id.slot,R.id.instructor });
setListAdapter(adapter);

您从数据库中获取数据,然后可以在执行期间填充Timetablelist。但是不需要每次都初始化适配器和setListAdapter。

编辑:

HashMap<String, String> hashMap ;
ArrayList<HashMap<String, String>> Timetablelist = new ArrayList<HashMap<String,String>>();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
    String subject = c.getString(c.getColumnIndex(dba.KEY_SUBJECT));
    String day = c.getString(c.getColumnIndex(dba.KEY_DAY));
    String slot = c.getString(c.getColumnIndex(dba.KEY_SLOT));
    String instructor = c.getString(c.getColumnIndex(dba.KEY_INSTRUCTOR_NAME));           
    // adding each child node to HashMap key => value
    hashMap = new HashMap<String, String>();
    hashMap.put("Subject", subject);
    hashMap.put("Day", day);
    hashMap.put("Slot", slot);
    hashMap.put("Instructor", instructor);
    Timetablelist.add(hashMap);
}
 ListAdapter adapter = new SimpleAdapter(
                            this, Timetablelist,R.layout.list_item, new String[] { "Subject", "Day",
                            "Slot","Instructor" }, new int[] { R.id.subject,
                            R.id.day, R.id.slot,R.id.instructor });
 setListAdapter(adapter);

【讨论】:

  • 试过了。将此代码 sn-p 移到 while(c.moveToNext()); .还是一样。
  • @Sash_kp 也将其移出 if 部分。如果您将其移到该代码下方,则您的列表尚未填充。
  • @Sash_kp 现在也检查我的编辑。你有 hashmap 的arraylist。每次从数据库中获取新数据时都需要 hashmap
  • private ArrayList&lt;String&gt; results = new ArrayList&lt;String&gt;(); //result contains the data of each row one by one TextView tView = new TextView(this); tView.setText("This data is stored in your phone's Database!"); getListView().addHeaderView(tView); setListAdapter(new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, results)); getListView().setTextFilterEnabled(true); 如果我喜欢这样做,它工作正常。但这是正常的 Listview 实现。我想以我的自定义 listview 方式显示它。
  • @Sash_kp 检查编辑后的代码。它应该工作。请不要在评论中发布代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
相关资源
最近更新 更多