Question

I am a new Android developer and I want to show contact numbers and contact name in a list but when only contact name show app running correctly but when I show with contact numbers then app is crashing I am using custom Adapter please can any one help me

my contactList activity is following

public class ContactList extends Activity {
    ListView lvContacts;
      List<String> listname;  
      List<String> listnumber;
    SimpleCursorAdapter adapter;
    ArrayList<PhoneList> arr;
    ArrayList<PhoneList> arrnumber;
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Cursor c1=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI , null, null, null, null);
            // Let activity manage the cursor
            //startManagingCursor(c1);


            listname = new ArrayList<String>();
            listnumber = new ArrayList<String>();
            c1.moveToFirst();
            while(c1.moveToNext())
            {
                Log.d("fghfh", ""+c1.getColumnIndex(Phone.NUMBER));
                String name=c1.getString(c1.getColumnIndex(Phone.DISPLAY_NAME));
                String  number=c1.getString(c1.getColumnIndex(Phone.NUMBER));
                //System.out.println(".................."+number); 


                 listname.add(name);
                listnumber.add(number);


               // Log.d("Contacts : ",name);//+":"+number);
            }
             Object[] obj=listname.toArray();
             Object[] objnumber=listnumber.toArray();
          //   arrnumber=new ArrayList<PhoneList>();
             arr=new ArrayList<PhoneList>();
             String[] da=new String[obj.length];
             String[] danumber=new String[objnumber.length];

             for(int i=0; i<obj.length; i++)
            {    
                 danumber[i]=(String)objnumber[i];
                 da[i]=(String)obj[i];
                 Toast.makeText(this, "Humayoon    zoom"+obj[i], Toast.LENGTH_SHORT).show();
                 arr.add(new PhoneList(da[i],danumber[i]));


            }
            ListView listView = (ListView) findViewById(R.id.lvContacts);
            CustomAdapter adpttt=new CustomAdapter(ContactList.this,R.layout.contacts_list_item,arr);
            listView.setAdapter(adpttt);
}
}

My Custom Adapter is following

public class CustomAdapter extends ArrayAdapter<PhoneList> {
    int inflatr;
    Context ctxt;
    ArrayList<PhoneList> data=new ArrayList<PhoneList>();
    public CustomAdapter(Context context, int resource, ArrayList<PhoneList> arr) {

        super(context, resource, arr);
        this.inflatr = resource;
        this.ctxt = context;
        this.data= arr;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserHolder holder = null;
         View row = convertView;
        if(convertView==null)
        {
             LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
             row = inflater.inflate(inflatr, parent, false);
             holder = new UserHolder();
             holder.textName=(TextView)row.findViewById(R.id.lblName);
             holder.stnumber=(TextView)row.findViewById(R.id.lblNumber);
//           holder.btnEdit = (ImageButton) row.findViewById(R.id.atomPay_removePay);
//           row.setTag(holder);
        }
        else
        {
             holder = (UserHolder) row.getTag();            
        }


         PhoneList dta=data.get(position);
        Toast.makeText(ctxt, "Humayoon    zoooommmm"+dta.getNumber(), Toast.LENGTH_SHORT).show();

        for(int i=1; i<=256; i++)
        {
            holder.textName.setText(dta.getName());
        }
        for(int j=1; j<=256; j++)
        {
            holder.textName.setText(dta.getNumber());
        }
//      holder.btnEdit.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              Toast.makeText(ctxt, "Humayoon    Siddiqueeeeeeeeeeeeeeeeeee"+dta.getName(), Toast.LENGTH_SHORT).show();
//              Intent moreIntent=new Intent(getContext(),ContactList.class);
//              String tName=dta.getName();
//              moreIntent.putExtra("Template",tName);
//              v.getContext().startActivity(moreIntent);
//               // ctxt.startActivity(ctxt,ContactList.class);
//          }
//      });
        return row;
    }
    @Override
    public int getCount() {
//      // TODO Auto-generated method stub
        return data.size();
    }

    static class UserHolder {
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        ImageButton btnEdit;
        Button btnDelete;
        TextView stnumber;
        }


}

My Phonelist class is following

package com.example.sqliteapplication;

    public class PhoneList {
        String name;
        String number;
        public PhoneList(String string,String number) {
            super();
            this.name = string;
            this.number=number;
        }
        public String getName() {
            return name;
            }
        public void setNumber(String number)
        {
            this.number=number;
        }
        public String getNumber()
        {
            return number;
        }
        public void setName(String name) {
            this.name = name;
            }



    }

error are

05-12 11:01:42.434: D/fghfh(2720): -1
05-12 11:01:42.451: E/CursorWindow(2720): Failed to read row 1, column -1 from a CursorWindow which has 2 rows, 34 columns.
05-12 11:01:42.452: D/AndroidRuntime(2720): Shutting down VM
05-12 11:01:42.452: W/dalvikvm(2720): threadid=1: thread exiting with uncaught exception (group=0x40f219a8)
05-12 11:01:42.465: E/AndroidRuntime(2720): FATAL EXCEPTION: main
05-12 11:01:42.465: E/AndroidRuntime(2720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqliteapplication/com.example.sqliteapplication.ContactList}: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.os.Looper.loop(Looper.java:153)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.main(ActivityThread.java:5299)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at java.lang.reflect.Method.invoke(Method.java:511)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at dalvik.system.NativeStart.main(Native Method)
05-12 11:01:42.465: E/AndroidRuntime(2720): Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.database.CursorWindow.nativeGetString(Native Method)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.database.CursorWindow.getString(CursorWindow.java:434)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.database.CursorWrapper.getString(CursorWrapper.java:114)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at com.example.sqliteapplication.ContactList.onCreate(ContactList.java:47)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.Activity.performCreate(Activity.java:5122)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-12 11:01:42.465: E/AndroidRuntime(2720):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
05-12 11:01:42.465: E/AndroidRuntime(2720):     ... 11 more
Was it helpful?

Solution

i think the problem is for projection in your ContentResolver query You don't have your Number in your query so try :

String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID};

        mCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

OTHER TIPS

Chances are that you've added a column but the test device still has a database file without that column. Just uninstall your app or clear its data to remove the old database file and make your onCreate() recreate the database on the next run.

So kindly Reinstall the app

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top