简体   繁体   English

无法在ListView中设置多项选择

[英]Can't set multiple choice selection in ListView

I'm having problems with setting multiple choice selection in ListView. 我在ListView中设置多项选择时遇到问题。 My ListView has contacts' names which are retrieved from the android phone. 我的ListView有从android手机中检索到的联系人姓名。

I've followed examples on setting multiple choice but it didn't work out. 我已经按照设置多项选择的例子,但没有成功。

I've tried typing 我试过打字

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        this.setListAdapter(adapter);

but I don't see the checkbox in the ListView. 但我没有看到ListView中的复选框。

And I've replaced R.layout.contacts_list with android.R.layout.simple_list_item_multiple_choice and it showed checkboxes but no names from the android phone's contacts. 我用android.R.layout.simple_list_item_multiple_choice替换了R.layout.contacts_list ,它显示了复选框但没有来自android手机联系人的名字。

Here are my codes: contacts_list.xml 这是我的代码:contacts_list.xml

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:stackFromBottom="false"
        android:transcriptMode="normal"
        android:choiceMode="multipleChoice" >
    </ListView>

    <TextView
        android:textColor="@color/dark_purple"
        android:id="@+id/contactName"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

ContactsList.java ContactsList.java

public class ContactsList extends ListActivity
{
    final Context context = this;

    Cursor cursor;

    String[] buddiesList = 
        {"Kanak Priya",
        "Joanne Liew",
        "Michelle Lam",
        "Teo Kin Hua",
        "Melissa Haiting",
        "David Yeo",
        "Natasha Akhbar",
        "Gillian Gan",
        "Sonia",
        "Ms Long",
        "Joan Tang",
        "Stephanie",
        "Nur Ashiqin"
        };

    BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);

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

        ListView list = getListView();



        setListAdapter(new ArrayAdapter<String> (this, R.layout.contacts_list, R.id.contactName, buddiesList));

        Uri allContacts = Uri.parse("content://contacts/people");

        Cursor c = managedQuery(allContacts, null, null, null, null);

        String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
        int[] views = new int[]  {R.id.contactName};

        startManagingCursor(c);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        this.setListAdapter(adapter);


        /*if(cursor.getCount()<0)
        {
            Intent displayIntent = new Intent(Intent.ACTION_VIEW);
            displayIntent.setData(Uri.parse("content://contacts/people"));
            startActivity(displayIntent);

            displayIntent = new Intent(Intent.ACTION_VIEW);
            //displayIntent.setData(Uri.parse("content://contacts/people/"+id));
            startActivity(displayIntent);
        }
        else
        {
            final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("ALERT!");
            alertDialog.setMessage("NO Name is Found! D:");
            alertDialog.setIcon(R.drawable.warning);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {
                    Intent backIntent = new Intent(context, ContactsList.class);
                    startActivity(backIntent);
                }
            });
            alertDialog.show();
        }*/
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id)
    {
        buddyDB.open();
        long name_id;
        super.onListItemClick(l, v, position, id);


        Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();

        TextView contactName = (TextView) v.findViewById(R.id.contactName);
        String NameValue = contactName.getText().toString();

        name_id = buddyDB.insertNames(NameValue);


        Toast.makeText(getBaseContext(),
                "Selected: " + buddiesList[position], Toast.LENGTH_SHORT).show();


        buddyDB.close();

Oh by the way, the 顺便说一句,哦

String[] buddiesList = 
        {"Kanak Priya",
        "Joanne Liew",
        "Michelle Lam",
        "Teo Kin Hua",
        "Melissa Haiting",
        "David Yeo",
        "Natasha Akhbar",
        "Gillian Gan",
        "Sonia",
        "Ms Long",
        "Joan Tang",
        "Stephanie",
        "Nur Ashiqin"
        };

is for my Toast message. 是我的Toast消息。

I really need help with this. 我真的需要帮助。 Any help will be appreciated. 任何帮助将不胜感激。 Examples will be greatly appreciated too. 例子也将非常感激。
Thanks 谢谢

You may have two choices: 您可能有两种选择:

1 - Use default layout from Android resources (simple_list_item_multiple_choice) 1 - 使用Android资源的默认布局(simple_list_item_multiple_choice)

you may replace this code: 你可以替换这段代码:

 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.contacts_list, c, columns, views); 

to: 至:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, c, columns, new int {android.R.id.text1});

2 - Create your new row layout contains your defined checkbox and handle it by your own. 2 - 创建新的行布局包含您定义的复选框并由您自己处理。

contact_list.xml contact_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/light_goldenrod"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:stackFromBottom="false"
        android:transcriptMode="normal"
        android:choiceMode="multipleChoice" >
    </ListView>
</LinearLayout>

contact_entry.xml contact_entry.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
    <TextView
        android:textColor="@color/dark_purple"
        android:id="@+id/contactName"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <CheckBox
        android:id="@+id/contactCheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:checked="false" />

</LinearLayout>

Add custom cursor adapter like this: 像这样添加自定义游标适配器:

public class YourCursorAdapter extends CursorAdapter  {

private Context mContext;
private Cursor mCurser;

public YourCursorAdapter(Context context, int layout, Cursor c, String[] from,
        int[] to, ContentResolver co) {
    super(context, c);
    mCurser = c;
    mContext = context;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View v = inflater.inflate(R.layout.contact_entry, parent, false);
    bindView(v, mContext, cursor);
    return v;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView contactName= (TextView)view.findViewById(R.id.contactName);
    contactName.setText(cursor.getString((mCurser
            .getColumnIndexOrThrow(Contacts.Phones.DISPLAY_NAME))));
    CheckBox contactCheckbox = (TextView)view.findViewById(R.id.contactCheckbox);
    contactCheckbox.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Checkbox checkBox = (Checkbox) v.findViewById(R.id.contactCheckbox)
            if (checkBox.isChecked())
                    checkBox.setChecked(false);
            else
                    checkBox.setChecked(true);
        }
    });
}

in your ListActivity: 在ListActivity中:

    YourCursorAdapter adapter = new YourCursorAdapter(this,
            null, c, null, null,null);
    this.setListAdapter(adapter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM