从列表视图中标记和选择项目

时间:2013-02-06 04:28:17

标签: android

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

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

在Android中有可能吗?如果是的话请帮助我。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

您可以看到this demo从listView中选择多个项目。 对于获取该列表视图中的竞争,您可以看到thisthis演示。

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

随意发表评论。

答案 2 :(得分:0)

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

herehere