Android 如何以字符串形式获取选中的联系人详细信息

Android 如何以字符串形式获取选中的联系人详细信息,android,listview,checkbox,Android,Listview,Checkbox,我希望在用户选中联系人时,以字符串形式获取联系人号码,然后按BackPress键将选定的值存储在数据库中。用户可以一次选择多个联系人,当用户返回活动时,复选框保持选中状态,以便他可以查看选定的联系人。 main活动 public class MainActivity extends Activity { String[] cellArray = null; String contacts; String phoneNumber, na

我希望在用户选中联系人时,以字符串形式获取联系人号码,然后按BackPress键将选定的值存储在数据库中。用户可以一次选择多个联系人,当用户返回活动时,复选框保持选中状态,以便他可以查看选定的联系人。 main活动

 public class MainActivity extends Activity {

        String[] cellArray = null;


        String contacts;



        String phoneNumber, name;
        ArrayList<String> phno0 = new ArrayList<String>();

        StringBuilder b = new StringBuilder();
        //private ArrayAdapter<String> adapter;
        List<String> arrayListNames;
        private ListView listview;
        private EditText edittext;
        private List<ProfileBean> list; 
        private SearchableAdapter adapter ;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            listview = (ListView) findViewById(R.id.listview);
            edittext  = (EditText) findViewById(R.id.edittext);

            list = new ArrayList<ProfileBean>();


            getAllCallLogs(this.getContentResolver());

            adapter = new SearchableAdapter(getApplicationContext(), list);
            listview.setAdapter(adapter);


            edittext.addTextChangedListener(new TextWatcher(){
                @Override
                public void afterTextChanged(Editable arg0) {

                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    adapter.getFilter().filter(s.toString());
                }
            });


            listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    ProfileBean bean = (ProfileBean) arg1.getTag();

                    Toast.makeText(getApplicationContext(), bean.getName(), Toast.LENGTH_LONG).show();
                }
            });
        }
        public void getAllCallLogs(ContentResolver cr) {

            Cursor phones = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                    null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " ASC");
            while (phones.moveToNext()) {
                phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                name = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));


                list.add(new ProfileBean(name, phoneNumber));


            }
        }

    }

代码有什么问题?问题是我不知道如何检索选中的值。根据它们的位置以字符串形式获取它们。您可以使用一些标志值从数据库获取值。例如:在插入时,将该标志保留为数据库中的一个字段,如果选中,则返回相同的值或执行类似操作
public class SearchableAdapter extends BaseAdapter implements Filterable, OnCheckedChangeListener {


    private List<ProfileBean>originalData = null;
    private List<ProfileBean>filteredData = null;
    private LayoutInflater mInflater;
    private ItemFilter mFilter = new ItemFilter();

    public SearchableAdapter(Context context, List<ProfileBean> data) {
        //mCheckStates = new SparseBooleanArray(filteredData.size());

        this.filteredData = data ;
        this.originalData = data ;
        mInflater = LayoutInflater.from(context);
    }


    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);


            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.lname = (TextView) convertView.findViewById(R.id.lname);
            holder.no = (CheckBox) convertView.findViewById(R.id.no);
            holder.no.setTag(position);

            holder.no.setOnCheckedChangeListener(this);


            convertView.setTag(R.layout.list_item,holder);
        } else {

            holder = (ViewHolder) convertView.getTag(R.layout.list_item);
        }



        ProfileBean bean = filteredData.get(position);
        holder.name.setText(bean.getName());
        holder.lname.setText(bean.getLname());

        convertView.setTag(bean);

        return convertView;
    }

    static class ViewHolder {
        TextView name;
        TextView lname;
        CheckBox no;
    }





    public Filter getFilter() {
        return mFilter;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<ProfileBean> list = originalData;

            int count = list.size();
            final ArrayList<ProfileBean> nlist = new ArrayList<ProfileBean>(count);

            String filterableString ;

            for (int i = 0; i < count; i++) {

                ProfileBean bean =  list.get(i);
                filterableString = bean.getName();
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(bean);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }
package com.example.mylistviewtest;


public class ProfileBean {
    private String name;
    private String lname;
    private boolean checked = false ;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }


    public ProfileBean(String name, String lname) {
        super();
        this.name = name;
        this.lname = lname;

    }
      public boolean isChecked() {
          return checked;
        }
        public void setChecked(boolean checked) {
          this.checked = checked;
        }

        public void toggleChecked() {
            checked = !checked ;
          }


        }