2014-10-05 47 views
0

getAllContacts方法(工作精細)數據ListView中未示出

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()「,在陣列「+ contactList);被示出:

在陣列

[{的ContactID:= 9,名字:= C,姓:= X,是homeAddress:= C,EMAILADDRESS:= C,電話號碼:= 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> 

任何幫助,將不勝感激

回答

0

問題是你通過this,而不是在ListAdapter getApplicationContext()

只需在您的列表適配器中更改此項即可。

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

更多瞭解,請查看這個答案Link

+0

我試過了,仍然連續被添加到ListView控件但它是空的 – 2014-10-05 18:17:32

+0

我檢查了所有的代碼粘貼在評論它dosen't似乎任何錯誤,而不是佈局,可以喲你給我佈局和Java代碼,所以我可以理解並嘗試從我身邊。謝謝 – 2014-10-05 22:13:08

0

問題是

 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); 

應該是沒有: