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(),&#34; in array&#34; + 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>
任何帮助都将不胜感激
答案 0 :(得分:0)
问题是您在ListAdapter中传递了this
而不是getApplicationContext()
。
只需在列表适配器中使用这样的转换器。
ListAdapter adapter=new SimpleAdapter(getApplicationContext(),contactList,R.layout.contact_entry,from,to);
要了解更多信息,请查看此答案Link
答案 1 :(得分: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);
应该没有: