0

getAllContacts Method (Working Fine)

 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); is showing:

in array[{contactId:=9, lastName:=c, firstName:=x, homeAddress:=c, emailAddress:=c, phoneNumber:=7}]

so it is receiving the contact details from the database.

Also, a row is created in the ListView but it is empty and the id, first name, last name are not showing and i am having trouble finding out why.

       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>

Any HELP would be appreciated

Rashad.Z
  • 2,494
  • 2
  • 27
  • 58

2 Answers2

0

problem is you pass this instead of getApplicationContext() in ListAdapter.

Just changer like this in your list adapter.

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

For more understand, check this answer Link

Community
  • 1
  • 1
Rahul Mandaliya
  • 738
  • 1
  • 12
  • 36
  • i tried that and still a row is adding to ListView but it is empty – Rashad.Z Oct 05 '14 at 18:17
  • i checked all of code pasted in comment its dosen't seems any mistake instead of layout, can you give me layout and java code so i can understand and try from my side. thanks – Rahul Mandaliya Oct 05 '14 at 22:13
0

The problem was

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

should be without the :

Rashad.Z
  • 2,494
  • 2
  • 27
  • 58