ListView与复选框在android中使用multichoice

时间:2012-11-14 10:31:25

标签: android listview checkbox

这是我的代码,当我点击按钮“b”时,listview填充每个项目的复选框,现在我想获取带有复选框的项目检查到另一个活动,我如何实现这一点,我来了一半我我很困惑如何做剩下的部分

这是我的代码  XML文件:      

<Button
    android:id="@+id/b"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pick contact" />

<ListView
    android:choiceMode="multipleChoice"
    android:id="@+id/lv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

这是java代码:

import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
 import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
Button b;
int PICK_CONTACT;
ArrayList<String> al;
ArrayAdapter<String> aa;
ListView lv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.b);

    lv = (ListView) findViewById(R.id.lv);
    al = new ArrayList<String>();



    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            displaycontacts();
            System.out.println("size" + al.size());
            aa = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_list_item_multiple_choice, al);
            lv.setAdapter(aa);


        }
    });

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position ,long arg3) 
        {

            //here i should get the item which is checked
        }
    });

}

public void displaycontacts() {
    try {
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);

        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {

                String name = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer
                        .parseInt(cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    System.out.println("name : " + name);
                    al.add(name);

                }
            }

        }

    } catch (Exception e) {
        System.out.println("Error:::::::::::::::::::" + e);
    }

}
}

4 个答案:

答案 0 :(得分:6)

尝试此链接以获得具有多项选择的列表视图。 http://android-coding.blogspot.in/2011/09/listview-with-multiple-choice.html。 让我知道你的问题是否得到解决?

答案 1 :(得分:1)

您需要比显示名称更多的信息来标识联系人。因此,请考虑使用CursorAdapter获取并保留该信息。

如果您正在使用联系人,则可能需要使用ContactsContract.ContactsColumns.LOOKUP_KEY来识别联系人。在那种情况下......

  • 使用ListView.getCheckedItemPositions()获取已检查项目的位置
  • 然后使用ListView.getItemAtPosition(int)将Cursor移动到该位置
  • 获取联系人的查找值

    cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
    
  • 并将查找值的数组/列表放在用于启动活动的intent的附加内容中。

答案 2 :(得分:0)

如果您坚持将参数从一个活动传递到另一个活动,您可以查看以下链接:http://developer.android.com/guide/faq/framework.html#3

答案 3 :(得分:0)

试试这个例子,你需要:

  • 接口
  • 数据(字符串文本,选择布尔值)
  • 适配器
  • 片段或活动
  • list_item.xml

您可以从数据开始:

public class MultipleData {
    private String text;
    private boolean selected;

    public MultipleData(String text, boolean selected) {
        this.text = text;
        this.selected = selected;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/list_item_text"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:text="@string/app_name"
        android:layout_toLeftOf="@+id/list_item_check_button"
        android:gravity="center"/>

    <RadioButton
        android:id="@+id/list_item_check_button"
        android:layout_width="wrap_content"
        android:layout_height="90dp"
        android:layout_alignParentRight="true"
        android:checked="false"
        android:clickable="false"
        android:focusable="false" />

</RelativeLayout>

activity_mail.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <ListView
            android:id="@+id/fragment_multiple_list_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</RelativeLayout>

适配器

public class MultipleAdapter extends BaseAdapter implements SelectedIndex {

    private final Context mContext;
    List<MultipleData> mList;

    @Override
    public void setSelectedIndex(int position) {

        if (mList.get(position).isSelected()) {
            mList.get(position).setSelected(false);
        } else {
            mList.get(position).setSelected(true);
        }
    }

    static class ViewHolder {
        TextView mTextView;
        RadioButton mRadioButton;
    }

    public MultipleAdapter(Context context, List<MultipleData> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.list_item, null);

            ViewHolder viewHolder = new ViewHolder();
            viewHolder.mTextView = (TextView) rowView.findViewById(R.id.list_item_text);
            viewHolder.mRadioButton = (RadioButton) rowView.findViewById(R.id.list_item_check_button);

            rowView.setTag(viewHolder);
        }

        // fill data
        ViewHolder holder = (ViewHolder) rowView.getTag();
        holder.mTextView.setText(mList.get(position).getText());
        holder.mRadioButton.setChecked(mList.get(position).isSelected());

        return rowView;
    }

}

接口

public interface SelectedIndex {
    void setSelectedIndex(int position);
}

在您的片段或活动中:

public class MultipleFragment extends Fragment implements AdapterView.OnItemClickListener {

    MultipleAdapter mAdapter;

    public MultipleFragment() {
        // Required empty public constructor
    }

    public static MultipleFragment newInstance() {
        return new MultipleFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_multiple, container, false);
        ListView listView = (ListView) view.findViewById(R.id.fragment_multiple_list_view);

        MultipleData android = new MultipleData("Android", false);
        MultipleData iPhone = new MultipleData("iPhone", false);
        MultipleData windowsMobile = new MultipleData("WindowsMobile", false);

        MultipleData blackberry = new MultipleData("Blackberry", false);
        MultipleData webOS = new MultipleData("WebOS", false);
        MultipleData ubuntu = new MultipleData("Ubuntu", false);

        MultipleData windows7 = new MultipleData("Windows7", false);
        MultipleData max = new MultipleData("Max OS X", false);
        MultipleData linux = new MultipleData("Linux", false);

        MultipleData os = new MultipleData("OS/2", false);
        MultipleData symbian = new MultipleData("Symbian", false);

        List<MultipleData> list = new ArrayList<>();
        list.add(0, android);
        list.add(1, iPhone);
        list.add(2, windowsMobile);

        list.add(3, blackberry);
        list.add(4, webOS);
        list.add(5, ubuntu);

        list.add(6, windows7);
        list.add(7, max);
        list.add(8, linux);

        list.add(9, os);
        list.add(10, symbian);

        mAdapter = new MultipleAdapter(getActivity(),list);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(this);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        mAdapter.setSelectedIndex(position);
        mAdapter.notifyDataSetChanged();
    }
}

您可以在GitHub和此blog

中查看此示例