Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用HashMap实现simpledapter_Android_Android Listview_Hashmap_Simpleadapter - Fatal编程技术网

Android 使用HashMap实现simpledapter

Android 使用HashMap实现simpledapter,android,android-listview,hashmap,simpleadapter,Android,Android Listview,Hashmap,Simpleadapter,我正在从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:ori

我正在从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>

我在下面的代码片段中遇到了问题。我尝试使用普通listview从sqlite检索数据,它工作得很好。但是当我使用SimpleAdapter将此代码片段用于自定义listview时,我只得到查询结果的最后一行。最后一行的数据在listview中重复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());

                       }  
HashMap HashMap=newhashmap();
ArrayList时间表列表;
//c是光标
if(c.moveToFirst()){
做{
stringsubject=c.getString(c.getColumnIndex(dba.KEY_subject));
stringday=c.getString(c.getColumnIndex(dba.KEY_day));
stringslot=c.getString(c.getColumnIndex(dba.KEY_slot));
字符串讲师=c.getString(c.getColumnIndex(dba.KEY_讲师_NAME));
//将每个子节点添加到HashMap key=>value
hashMap.put(“主题”,主题);
hashMap.put(“日”,日);
hashMap.put(“Slot”,Slot);
hashMap.put(“讲师”,讲师);
Timetablelist.add(hashMap);
ListAdapter=新的SimpleAdapter(
这个,Timetablelist,R.layout.list_项,新字符串[]{“Subject”,“Day”,
“插槽”,“讲师”},新int[]{R.id.subject,
R.id.day,R.id.slot,R.id.讲师});
setListAdapter(适配器);
}而(c.moveToNext());
}  

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

将下面的内容移出

 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);
您可以从数据库中获取数据,并可以在do while中填充
Timetablelist
。但不需要每次初始化适配器和setListAdapter

编辑:

HashMap-HashMap;
ArrayList Timetablelist=新建ArrayList();
对于(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
stringsubject=c.getString(c.getColumnIndex(dba.KEY_subject));
stringday=c.getString(c.getColumnIndex(dba.KEY_day));
stringslot=c.getString(c.getColumnIndex(dba.KEY_slot));
字符串讲师=c.getString(c.getColumnIndex(dba.KEY_讲师_NAME));
//将每个子节点添加到HashMap key=>value
hashMap=新的hashMap();
hashMap.put(“主题”,主题);
hashMap.put(“日”,日);
hashMap.put(“Slot”,Slot);
hashMap.put(“讲师”,讲师);
Timetablelist.add(hashMap);
}
ListAdapter=新的SimpleAdapter(
这个,Timetablelist,R.layout.list_项,新字符串[]{“Subject”,“Day”,
“插槽”,“讲师”},新int[]{R.id.subject,
R.id.day,R.id.slot,R.id.讲师});
setListAdapter(适配器);

将setListAdapter移出do while并移动适配器实例化:
ListAdapter=new SimpleAdapter(
将该代码段移到do while循环下的右侧,但仍保持不变。最后一行在listview中重复8次。尝试了此操作。将该代码段移到while(c.moveToNext())下的右侧.仍然相同。@Sash\u kp也将其移出if部分。如果将其移到该代码下面,则列表尚未填充。@Sash\u kp现在也检查我的编辑。您有hashmap的arraylist。每次从数据库获得新数据时都需要hashmap
private arraylist results=new arraylist();//结果包含每一行的数据TextView tView=new TextView(此);tView.setText(“此数据存储在手机的数据库中!”);getListView().addHeaderView(tView);setListAdapter(新ArrayAdapter(此,android.R.layout.simple_list_item_1,results));getListView().setTextFilterEnabled(true)
如果我喜欢,它可以正常工作。但这是正常的Listview实现。我想以自定义Listview的方式显示它。@Sash\u kp检查编辑的代码。它应该可以工作。请不要在注释中发布代码。
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);