如何在字符串中获取已检查的联系人详细信息

时间:2014-04-03 10:04:42

标签: android listview checkbox

我想在用户检查联系人时获取字符串中的联系人号码,之后 onBackpress 所选值可以存储在database.user中,可以一次选择多个联系人,当用户返回时回到活动,复选框保持选中状态,以便他可以看到他选择的联系人。 MainActivity

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


            }
        }

    }

Adapterclass

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

BeanClass

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


        }

0 个答案:

没有答案