0

I have this following code

public class Contacts extends ListActivity {

    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor cursor = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        startManagingCursor(cursor);

        String[] from = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone._ID };

        int[] to = { android.R.id.text1, android.R.id.text2 };

        ListAdapter listadapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_multiple_choice, cursor,
                from, to);

        setListAdapter(listadapter);

        lv = getListView();

        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        lv.setItemsCanFocus(false);
        lv.setTextFilterEnabled(true);

        final int check = lv.getCheckedItemPosition();//lv.getCheckedItemIds();



        lv.setOnItemClickListener(new OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                TextView tv = (TextView) arg1.findViewById(android.R.id.text2);


                String aa = (String) tv.getText(); 

                String ab =  lv.getItemAtPosition(arg2).toString();
                int ac = lv.getSelectedItemPosition();
                System.out.println("wow "+ab +" ww "+ ac + " "+ aa);

                Toast.makeText(Contacts.this, ((TextView) arg1).getText(), Toast.LENGTH_LONG).show();       
            }
        });

    }

}

I want to read the Number part from ListView so far I am not been able to do it. I even tried TextView tv = (TextView) arg1.findViewById(android.R.id.text2); String test = tv.getText().toString(); but I always get NullPointerException basically I want to bring all the contacts from my phone's Contact list so that I could select multiple contacts at a time. Help please!

Ahmed S. Durrani
  • 1,535
  • 2
  • 17
  • 41

4 Answers4

1

The problem is that you are using android.R.layout.simple_list_item_multiple_choice, which doesn't contain any TextView with id android.R.id.text2. The layout contains this (at least on some devices):

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="8dip"
    android:paddingRight="8dip"
/>

You should create your custom layout for the list item.

And also note that it's generally a bad idea to use private layouts and other resources (those in android.R class), you can't be sure that they are present on all devices and versions of Android.

Tomik
  • 23,857
  • 8
  • 121
  • 100
0

If the data you want to get is in the ListView you have the same data on the ListAdapter, so on itemClick you can retrieve the corresponding object having the view id of the clicked item.

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    int id = arg1.getItemId();
    listadapter.getItem(id);
    ...
    ...
}
gpasci
  • 1,420
  • 11
  • 16
0

i guess setcontentview(R.id.yourlayout) is not created in oncreate() so the program gives you null pointer exception everytime... just set setcontentview(R.id.yourlayout) and i guess it will work

karan
  • 8,637
  • 3
  • 41
  • 78
0

see the signature of onItemClick :

public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)

you can use the third one as the position of the item being selected, and call getItem() with this value as the parameter.

no matter what you do , don't use the findViewById since it's just wrong for listview . the reason why it's wrong is that the listView re-uses old views in order to use the minimal number of views (about the same number of views that are shown at any time).


EDIT:

here's an example of what you can do with what i've written:

lv.setOnItemClickListener(new OnItemClickListener()
      {
        @Override
        public void onItemClick(final AdapterView<?> arg0,final View arg1,final int arg2,final long arg3)
          {
          final Object item=listadapter.getItem(arg2);
          final CursorWrapper cursorWrapper=(CursorWrapper)listadapter.getItem(arg2);
          final int columnCount=cursorWrapper.getColumnCount();
          for(int i=0;i<columnCount;++i)
            Log.d("DEBUG",cursorWrapper.getColumnName(i)+":"+cursorWrapper.getString(i));
          }
      });
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Thanks for the answer, but doing so returns no number just id of that selected path. I tried `.toString()` didnt help either. – Ahmed S. Durrani Feb 24 '13 at 11:29
  • @AhmedDurrani actually it does work . it returns an instance of a CursorWrapper. i've updated my answer to show how to get all the info you can use from this single item. btw, there are many deprecated methods and classes in your code. not sure what are the alternatives , but you can always use your own implementation (using baseAdapter). – – android developer Feb 26 '13 at 20:40