3

这是我的代码,当我单击按钮“b”时,列表视图会为每个项目填充一个复选框,现在我想将带有复选框的项目签入另一个活动,我如何实现这一点,我走了一半我很困惑剩下的部分怎么做

这是我的代码 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

3 回答 3

6

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

于 2012-11-14T10:43:44.000 回答
1

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

如果您正在使用联系人,您可能希望使用ContactsContract.ContactsColumns.LOOKUP_KEY来识别联系人。在这种情况下...

  • 使用ListView.getCheckedItemPositions()获取选中项的位置
  • 然后 ListView.getItemAtPosition(int) 让光标移动到那个位置
  • 获取联系人的查找值

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

于 2012-11-14T11:13:08.683 回答
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>

活动邮件.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和此博客中查看此示例

于 2017-01-27T17:26:57.000 回答