gpt4 book ai didi

java - 使用复选框过滤联系人并获取电话号码

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:02 25 4
gpt4 key购买 nike

我正在开发一款与所有 Android 手机上的默认短信应用类似的应用。我的问题是选择多个用户向其发送 SMS 消息。到目前为止我所做的是将我的联系人存储为带有复选框的 ListView 项目。现在我只需要从选定的联系人中获取电话号码。

那么我在做什么方面遇到了麻烦。1)从 ListView 中显示的联系人中提取电话号码2)在新 Activity 的 TextView 中显示该数字

抱歉,如果我的代码难以理解,请询问您是否需要澄清。

这是显示 ListView 的 XML,称为 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>

这是我的 Activity,它将所有内容组合在一起。

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

<?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 这实际上是我想输入数字的 TextView ,这样我就可以发送群发短信。

    <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>

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

最佳答案

我没有看过你的代码,但这是对我有用的机制:

1.setOnCheckedChangeListener 放入您的复选框。

2. 如果 CheckboxChecked 则将该联系人添加到 arraylist

3. 如果CheckBox 未选中,则从arraylist 中删除该联系人。

4. 使用 startActivityForResult() 开始您的联系人列表 Activity 并覆盖 onActivityResult()

5.离开联系人 Activity 之前,设置您的 Intent 中选定的联系人

6. 在您的 Activity 中接收选定的联系人。

7. 现在您已经选择了可以在 TextView 中显示的联系人。

注意:您需要为此使用自定义列表适配器:

自定义列表适配器:

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>

关于java - 使用复选框过滤联系人并获取电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com