使用复选框来过滤接触,并得到电话号码 [英] using checkbox to filter contacts and get phone number

查看:153
本文介绍了使用复选框来过滤接触,并得到电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序的工作原理类似于在所有Android手机的默认短信应用程序。我的问题是选择一个以上的用户发送SMS消息。我迄今所做的是存储在我的接触与复选框列表视图项。现在我只需要从选定的联系人取得电话号码。

I am working on an app that works similar to the default text messaging app on all Android phones. My problem is selecting more than one user to send an SMS message to. What I have done so far is stored my contacts as listview items with check boxes. Now I just need to get the phone numbers from the selected contacts.

所以我在做什么有麻烦。 1)从拉我的列表视图中显示的联系人的电话号码 2)显示这个数字在一个TextView在一个新的活动

So what I am having trouble on doing. 1) Pulling a phone number from the contacts displayed in my listview 2)displaying that number in a textview in a new activity

很抱歉,如果我的code是难以理解的,请询问您是否需要clerification。

Sorry if my code is difficult to understand, please ask if you need clerification.

这是在该列表视图中显示的XML,叫contact_manager.xml:

This is the XML in which the listview is shown, called contact_manager.xml:

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

        <ListView
            android:id="@+id/contactList"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/showInvisible"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/showInvisible" />

    </LinearLayout>

这是我的动态调用一切融合在一起。

This is my Activity that calls everything together.

public final class ContactManager extends Activity {

public static final String TAG = "ContactManager";

private ListView mContactList;
private boolean mShowInvisible;
private Button mShowInvisibleControl;

/**
 * Called when the activity is first created. Responsible for initializing
 * the UI.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.v(TAG, "Activity State: onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_manager);

    // Obtain handles to UI objects

    mContactList = (ListView) findViewById(R.id.contactList);
    mShowInvisibleControl = (Button) findViewById(R.id.showInvisible);

    // Initialize class properties
    mShowInvisible = false;
    // mShowInvisibleControl.setChecked(mShowInvisible);
    mShowInvisibleControl.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

        }
    });
    populateContactList();
}

/**
 * Populate the contact list based on account currently selected in the
 * account spinner.
 */
private void populateContactList() {
    // Build adapter with contact entries
    Cursor cursor = getContacts();
    String[] fields = new String[] { ContactsContract.Data.DISPLAY_NAME };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.contact_entry, cursor, fields,
            new int[] { R.id.contactEntryText });
    mContactList.setAdapter(adapter);
}

/**
 * Obtains the contact list for the currently selected account.
 * 
 * @return A cursor for for accessing the contact list.
 */
private Cursor getContacts() {
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
            + (mShowInvisible ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs,
            sortOrder);
}

contact_entry.xml

contact_entry.xml

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

    <CheckBox
        android:id="@+id/contactEntryText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/contactEntryText" />

</LinearLayout>

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

    <ListView
        android:id="@+id/contactList"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/showInvisible"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/showInvisible" />

</LinearLayout>

这是我invite_text.xml这实际上是我想要输入的数字变成这样我就可以发送一条群发短信文本视图。          

This is my invite_text.xml This is actually the text view i want to input the numbers into so i can send a mass text message.

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/contacts"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:paddingBottom="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:text="@string/contacts"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            <!-- android:textColor="#fff" android:background="@drawable/header" for header background -->

            <Button
                android:id="@+id/contactsButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:text="@string/contacts" />
        </RelativeLayout>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/enter_contact"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <AutoCompleteTextView
            android:id="@+id/contactnumber"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/to" >

            <requestFocus />
        </AutoCompleteTextView>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/message_to_send"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/invite_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/message_join" />

        <Button
            android:id="@+id/sendtxt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="doLaunchContactPicker"
            android:text="@string/send_txt" />
    </LinearLayout>

</ScrollView>

如果你需要我发布更多的信息,请询问。

If you need me to post any more information, please ask.

推荐答案

我没有看过你的code,但这里是为我工作的机制:

I have not looked at your code but here is the mechanism which worked for me:

1 setOnCheckedChangeListener 您的复选框。

2 如果复选框检查然后添加联系人的ArrayList

2. If Checkbox is Checked then add that contact to arraylist.

3。如果复选框选中然后删除该联系人的ArrayList

3. If CheckBox is unchecked then remove that contact from arraylist.

4。 startActivityForResult()启动您的联系人列表的活动并覆盖 onActivityResult()

5。 留下联系活性设置在你的的意图选定的联系人

6。接收您的活动选定的联系人。

6. Receive the selected contacts in your activity.

7。现在,您已经选择的联系人,你可以在的TextView 显示。

7. Now you have selected contacts which you can show in TextView.

注意::您需要使用自定义列表适配器这样的:

Note: You need to use custom list adapter for this:

自定义列表适配器:

public class YourAdapterName extends BaseAdapter{

private Context mContext;
private ArrayList<string> mValuestoShow;

/**
 * Constructor to be called to initialize adapter with values.
 * @param context
 * @param vector
 */
public YourAdapterName(Context context, ArrayList<string> contacts){
    mContext = context;
    mValuestoShow = contacts;
}

public int getCount() {
    if(null != mValuestoShow){
        return mValuestoShow.size();
    }
    return 0;
}

public Object getItem(int position) {
    if(position < mValuestoShow.size())
        return  mValuestoShow.get(position);
    else
        return null;
}

public long getItemId(int position) {
    return 0;
}

/**
 * This method can be override to enable/disable particular list row.
 */
@Override
public boolean isEnabled(int position) {
    //Write your code here......
    return super.isEnabled(position);
}

public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.contact_list_layout, null);
            holder = new ViewHolder();
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(text goes here ....);

        holder.checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if ( isChecked )
                    //Add contact...
                else
                    //Remove contact.
            }
        });

        return convertView;
    }

    class ViewHolder {
        TextView name;
        CheckBox checkbox;
    }

}

your_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="50dp"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="@android:color/black" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

这篇关于使用复选框来过滤接触,并得到电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆