1

我想从手机中获取联系人并将其显示为列表视图,我想选择一些并将其添加到数据库中。问题是我不想一一选择,而不是那样

我想标记联系人并整体选择。

在安卓中可以吗?如果是,请帮助我。

4

3 回答 3

3

您可以使用此代码:

public class ImportContactsFromContactsList extends ListActivity implements Runnable
{
     Cursor phoneCursor;
     Button done,cancel;

     final int PROGRESS_DIALOG=0;
     ProgressDialog progressDialog;
     ArrayAdapter<ContactModel> adapter;
    @Override
    public void onCreate(Bundle saveinst)
    {
    super.onCreate(saveinst);




     progressDialog=ProgressDialog.show(ImportContactsFromContactsList.this, "LoadContacts", "Please wait...", true, false);
     Thread loadContact=new Thread(this);
      loadContact.start();


}


public void run()
{
    List<ContactModel>contactList=new ArrayList<ContactModel>();
    try

{
        Looper.prepare();
     String[] projection = new String[] {
             ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.HAS_PHONE_NUMBER};
     String selection = ContactsContract.Contacts.DISPLAY_NAME + "!='null'";
     String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
     Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, selection, null, sortOrder);
     startManagingCursor(cursor);
     String personName=null;
     String number=null;
     if(cursor!=null)
     {
         while(cursor.moveToNext())
         {
             String id=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
             personName=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
             if (Integer.parseInt(cursor.getString(
                     cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
             {
                  Cursor pCur = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
            null, 
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
            new String[]{id}, null);
                  pCur.moveToFirst();
                  number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            pCur.close();
             }
             contactList.add(get(personName,number));


         }

         adapter=new MyContactListAdapter(ImportContactsFromContactsList.this, contactList);
         handler.sendEmptyMessage(0);
     }


     }
        catch(Exception exce)
        {
            exce.printStackTrace();
        }

}

private final Handler handler=new Handler(){

    @Override
    public void handleMessage(Message msg)
    {
        progressDialog.dismiss();
         setListAdapter(adapter);
         setContentView(R.layout.contactlist);

    }
};

public ContactModel get(String name,String number)
{
    return new ContactModel(name,number);
}

}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background1" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add Contacts In BlackList"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginTop="80dp"
        android:textSize="40sp"
        android:textAppearance="?android:attr/textAppearanceLarge" ></TextView>
    <FrameLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#302217"
        android:cacheColorHint="#333333"
        android:divider="#999966"
        android:dividerHeight="2dp"
        android:fastScrollEnabled="true" >

    </ListView>
    <TextView android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="There Is No Contact"
        android:textSize="30sp"
        android:textAppearance="?android:attr/textAppearanceLarge">
    </TextView>
    </FrameLayout>

</LinearLayout>

MyContactListAdapter.java 代码:

public class MyContactListAdapter extends ArrayAdapter<ContactModel>
{
    private final List<ContactModel> list;
    private final Activity context;
public MyContactListAdapter(Activity context,List<ContactModel> list)
{
    super(context,R.layout.contactliststyle,list);
    this.context=context;
    this.list=list;

}

public class ViewHolder
{
    protected TextView txtName,txtNumber;
    protected CheckBox selection ;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.contactliststyle, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.txtName = (TextView) view.findViewById(R.id.txtView_Name);
        viewHolder.txtNumber = (TextView)view.findViewById(R.id.txtView_Number);
        viewHolder.selection = (CheckBox)view.findViewById(R.id.checkbtn_selectContact);
        viewHolder.selection
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        ContactModel element = (ContactModel)viewHolder.selection
                                .getTag();
                        element.setSelected(buttonView.isChecked());
                        if(buttonView.isChecked())
                        {
                        String selectedName=element.getName();
                        String selectedNumber=element.getNumber();
                        Transactions trans=new Transactions(context);
                        trans.addBlackListNumberFromCallLog(selectedNumber, selectedName);

                        }
                        else
                        {
                            String selectedNumber=element.getNumber();
                            Transactions trans=new Transactions(context);
                            trans.dropBlackListNumber(selectedNumber);
                        }

                    }
                });
        view.setTag(viewHolder);
        viewHolder.selection.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).selection.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.txtName.setText(list.get(position).getName());
    holder.txtNumber.setText(list.get(position).getNumber());
    holder.selection.setChecked(list.get(position).isSelected());
    return view;
}

}

ContactModel.java 代码:就像getter setter一样简单。

public class ContactModel 
{

private String name;
private String number;
private boolean selected;
public ContactModel(String personName,String phNumber)
{
    this.name=personName;
    this.number=phNumber;
    selected=false;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getNumber() {
    return number;
}
public void setNumber(String number) {
    this.number = number;
}
public boolean isSelected() {
    return selected;
}
public void setSelected(boolean selected) {
    this.selected = selected;
}

}

ContactListStyle XML 布局代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/done_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Done"
        android:layout_gravity="center_horizontal" />

    <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp" >
    <TextView
        android:id="@+id/txtView_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textColor="#FFFFFF"
        android:textSize="20px" >
    </TextView>
     <CheckBox
        android:id="@+id/checkbtn_selectContact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px" >
    </CheckBox>
<TextView
        android:id="@+id/txtView_Number"
        android:layout_below="@id/txtView_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Number"
        android:textColor="#FFFFFF"
        android:textSize="20px" >
    </TextView>

</RelativeLayout>
</LinearLayout>
于 2013-02-06T05:39:09.057 回答
1

您可以查看此演示以从 listView 中选择多个项目。并且对于该列表视图中的获取内容,您可以看到这个这个演示。

希望您能从那里获得完整的信息。

随意发表评论。

于 2013-02-06T04:43:27.097 回答
0

如果我对您的问题很清楚,我建议您使用带有多选列表的 android 对话框。阅读并将联系人列表作为数组适配器的来源。如果需要,请阅读此示例。

这里这里

于 2013-02-06T04:49:36.293 回答