【问题标题】:data not showing in ListView数据未在 ListView 中显示
【发布时间】:2014-10-05 17:51:38
【问题描述】:

getAllContacts 方法(工作正常)

 public ArrayList<HashMap<String,String>> getAllContacts(){
 ArrayList<HashMap<String,String>> contactArrayList=new ArrayList<HashMap<String,String>>();
 String seletQuery="select * from contact ORDER by lastName";

 SQLiteDatabase database=this.getWritableDatabase();
 Cursor cursor=database.rawQuery(seletQuery, null);
 if(cursor.moveToFirst()) 
 {
     do{
         HashMap<String,String> contactMap=new HashMap<String,String>();
         contactMap.put("contactId:", cursor.getString(0));
         contactMap.put("firstName:", cursor.getString(1));
         contactMap.put("lastName:", cursor.getString(2));
         contactMap.put("phoneNumber:", cursor.getString(3));
         contactMap.put("emailAddress:", cursor.getString(4));
         contactMap.put("homeAddress:", cursor.getString(5));
         contactArrayList.add(contactMap);

     }
     while(cursor.moveToNext());
 } 
 return contactArrayList;

}

MainActivity.java

Log.e(this.getClass().getName(),"in array"+contactList);正在显示:

在数组中[{contactId:=9, lastName:=c, firstName:=x, homeAddress:=c, emailAddress:=c, phoneNumber:=7}]

所以它正在从数据库中接收联系方式。

另外,在 ListView 中创建了一行,但它是空的,并且没有显示 id、名字、姓氏,我无法找出原因。

       ListView lv= (ListView)findViewById(R.id.listview);
       ArrayList<HashMap<String, String>> contactList =  dbTools.getAllContacts();
        dbTools.getAllContacts();
         String[] from = new String[] {"contactId", "lastName", "firstName"};
          int[] to = new int[] { R.id.contactId,R.id.lastName,R.id.firstName};
        if(contactList.size()!=0){

            Log.e(this.getClass().getName(),"in array"+contactList);
            ListAdapter adapter=new     
          SimpleAdapter(this,contactList,R.layout.contact_entry,from,to);
            lv.setAdapter(adapter);
        }

main.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.addressbookapp.MainActivity$PlaceholderFragment" >
<TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    <ListView android:id="@+id/listview"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    </ListView>
 </TableRow>
 </TableLayout>

contact_entry.xml

  <?xml version="1.0" encoding="utf-8"?>
 <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
    <TextView android:id="@+id/contactId"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:width="20dip"
    />
    <TextView android:id="@+id/lastName"

        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
         android:padding="@dimen/padding_dp"
         android:textStyle="bold"
        android:width="100dip"
    />
    <TextView android:id="@+id/firstName"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:padding="@dimen/padding_dp"
        android:textStyle="bold"
        android:width="100dip"
    />
    </TableRow>

任何帮助将不胜感激

【问题讨论】:

    标签: java android


    【解决方案1】:

    问题是您在 ListAdapter 中传递 this 而不是 getApplicationContext()

    只需在您的列表适配器中使用这样的转换器。

    ListAdapter adapter=new SimpleAdapter(getApplicationContext(),contactList,R.layout.contact_entry,from,to);

    更多了解,请查看此答案Link

    【讨论】:

    • 我试过了,仍然有一行添加到 ListView 但它是空的
    • 我检查了所有粘贴在注释中的代码,它似乎没有任何错误而不是布局,你能给我布局和java代码,这样我就可以理解并从我这边尝试。谢谢
    【解决方案2】:

    问题是

         contactMap.put("contactId:", cursor.getString(0));
         contactMap.put("firstName:", cursor.getString(1));
         contactMap.put("lastName:", cursor.getString(2));
         contactMap.put("phoneNumber:", cursor.getString(3));
         contactMap.put("emailAddress:", cursor.getString(4));
         contactMap.put("homeAddress:", cursor.getString(5));
         contactArrayList.add(contactMap);
    

    应该没有:

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2021-06-25
      相关资源
      最近更新 更多