2012-01-03 56 views
2

我正在研究一个应用程序,在该应用程序中我需要从联系簿和显示中获取所有联系人。我想让用户选择一些联系人并将其添加到保存在数据库中的组中。无法显示阵列列表中的数据到自定义ArrayAdapter

我创建了一个自定义列表视图 - contactitem.xml-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 


android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal"> 


<TextView 

    android:id="@+id/contactname" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" 
    android:layout_marginLeft="20dp" 
    android:ellipsize="end"   
    android:singleLine="true" 
    android:clickable="true"/> 

<TextView 
    android:id="@+id/contactnum" 
    android:layout_width="wrap_content" 
    android:textColor="@color/White" 
    android:clickable="true" 
    android:layout_gravity="center_vertical" 
    android:layout_height="wrap_content"/> 

    <Button 
    android:id="@+id/add" 
    android:layout_width="wrap_content" 
    android:layout_height="35dp" 
    android:text="@string/add_contacts" 
    android:layout_gravity="center_vertical" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false"/> 

</LinearLayout> 

我已经从联系获取触点的SelectContact类书本 -

public class SelectContacts extends Activity implements OnClickListener{ 

    private String groupName; 
    private Button back; 
    private Button home; 
    private List<Contact> list = new ArrayList<Contact>(); 

    private ListView contactList; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.selectcontacts); 

      Bundle bundle = getIntent().getExtras(); 
      groupName=bundle.getString("groupName"); 

      back=(Button)findViewById(R.id.back_button); 
      back.setOnClickListener(this); 

      home=(Button)findViewById(R.id.home_button); 
      home.setOnClickListener(this); 

      contactList=(ListView)findViewById(R.id.contactsListView); 

      ContentResolver cr = getContentResolver(); 



      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

      cur.moveToFirst(); 

      if (cur.getCount() > 0) { 

       do{ 

        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 

        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 

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

        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, fields, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); 

        pCur.moveToFirst(); 

        do { 

         String number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

         Contact c=new Contact(name,number); 

         list.add(c); 

         }while (pCur.moveToNext()); 

         pCur.close(); 
        }    



        }while (cur.moveToNext()); 

       ContactAdapter contactAdapter=new ContactAdapter(this, R.layout.contactitem, list, contactList,groupName); 



       contactList.setAdapter(contactAdapter); 


      } 


     } 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intent= new Intent(); 

      if(v==back){ 

       Bundle bundle = new Bundle(); 
       bundle.putString("groupName", groupName); 
       intent.putExtras(bundle); 

       intent.setClass(SelectContacts.this, GroupDetails.class); 
       startActivity(intent); 

      } 
      if(v==home){ 
       intent.setClass(SelectContacts.this, Welcome.class); 
       startActivity(intent); 

      } 

     } 
} 

,并实现了自定义适配器 - ContactAdapter- `

public class ContactAdapter extends ArrayAdapter<HashMap<String, Contact>>{ 

private ArrayList<HashMap<String, Contact>> items; 

private LayoutInflater mInflater ; 

public ContactAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Contact>> items) { 

     super(context, textViewResourceId, items); 

     this.items = items; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

      mInflater = LayoutInflater.from(getContext()); 

      ViewHolder holder; 

     if(convertView==null){ 

      convertView = mInflater.inflate(R.layout.contactitem, parent, false); 

      holder = new ViewHolder(); 

      holder.name = (TextView) convertView.findViewById(R.id.contactname); 

      holder.number = (TextView) convertView.findViewById(R.id.contactnum); 

      holder.add=(Button)convertView.findViewById(R.id.add); 

      convertView.setTag(holder); 

     } 

     else{ 

      holder=(ViewHolder)convertView.getTag(); 

     } 

     String name=items.get(position).get(""+position).getContactName(); 

     String number=items.get(position).get(""+position).getContactNum(); 

     holder.name.setText(name); 

     holder.number.setText(number); 

     return convertView; 
} 



static class ViewHolder{ 

    TextView name; 

    TextView number; 

    Button add; 

} 

}

here Contact is a simple POJO- 



public class Contact { 
    private String contactName; 
    private String contactNum; 

    public String getContactName() { 
     return contactName; 
    } 
    public void setContactName(String contactName) { 
     this.contactName = contactName; 
    } 

    public String getContactNum() { 
     return contactNum; 
    } 
    public void setContactNum(String contactNum) { 
     this.contactNum = contactName; 
    } 

} 

我在Android的新手..

上面的代码工作在emulater安静罚款并获取联系人姓名和号码显示在列表中。但是当我测试的手机验证码,其Gmail帐户syncronized,它得到了与以下错误崩溃..

CursorIndexOutOfBoundsException:指数0请求,大小为0

我没有任何想法,为什么这发生了。请给出一些建议...

回答

1

看起来你忘记了在取得联系人光标后打电话给cur.moveToFirst();。此行之后,

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

你也在这里缺少同样的事情,在此之后 - pCur.moveToFirst();

Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); 
+1

作出了改变,但仍然得到相同的日志条目... :( – 2012-01-03 10:58:08

+0

你现在正在得到哪个错误更新logcat。 – 2012-01-03 10:59:50

+0

你也错过了另一个光标的同样的事情,检查我编辑的答案。 – 2012-01-03 11:18:02

1

使用以下功能:

  • cur.moveToFirst();
  • pCur.moveToFirst();
+0

它的工作..谢谢先生..但它仍然不能显示活动中的联系人..可能是我有我的适配器有问题... – 2012-01-03 11:19:33