Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

android - How to obtain the checked rows in a custom view list

Can any one tell me how can i obtain the values of the checked rows in a custom view list? For example my code has a custom view list with two text view and a checkbox. Whenever the user checks a row and hits the submit button i need to retrieve the information in the two textview..

The code is

 public class contacts extends Activity implements OnItemClickListener {
        static final String TAG = "contacts";
        ArrayList<String> contactName = new ArrayList<String>();
        ArrayList<String> contactNumber = new ArrayList<String>();

        MyAdapter myAdapter;
        Button AddNumber;
        String numberIntent;
        View vi;
        ListView listView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listview);

            getAllContacts(this.getContentResolver());
            listView = (ListView) findViewById(R.id.lists);
            myAdapter = new MyAdapter();
            listView.setAdapter(myAdapter);

            listView.setItemsCanFocus(false);
            listView.setTextFilterEnabled(true);
            // adding
            AddNumber = (Button) findViewById(R.id.AddNumbers);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {



                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    Object listItem = listView.getItemAtPosition(0);
                    Log.d(TAG,"listView listener" + listItem);
                }
            });

            AddNumber.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    StringBuilder checkedContacts = new StringBuilder();
                    Log.d(TAG, "onClick" + myAdapter.mCheckStates.size());
                    for (int i = 0; i < contactName.size(); i++)

                    {
                        if (myAdapter.mCheckStates.get(i) == true) {
                            checkedContacts.append(contactName.get(i).toString());
                            checkedContacts.append("
");

                        } else {
                            Log.d(TAG, "No OnClick" + contactName.get(i).toString());
                        }

                    }

                    Toast.makeText(contacts.this, checkedContacts,
                            Toast.LENGTH_LONG).show();
                     ArrayList<String> checkboxArray = new ArrayList<String>();
                     myAdapter.mCheckStates = listView.getCheckedItemPositions();
                     Log.d(TAG, "Sparse" + myAdapter.mCheckStates);
                     for (int i = 0; i < myAdapter.mCheckStates.size(); i++) {
                     int position = myAdapter.mCheckStates.keyAt(i);
                     boolean bool = myAdapter.mCheckStates.valueAt(position);
                                     Log.d(TAG, "Sparse2" + myAdapter.mCheckStates);
                     }
                     String[] outputStrArr = new String[checkboxArray.size()];

                     for (int i = 0; i < checkboxArray.size(); i++) {
                     outputStrArr[i] = checkboxArray.get(i);


                     }

                }
            });

        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            myAdapter.toggle(arg2);
        }

        public void getAllContacts(ContentResolver cr) {

            Cursor cursor = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                    null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                            + " COLLATE LOCALIZED ASC");
            while (cursor.moveToNext()) {
                String name = cursor
                        .getString(cursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = cursor
                        .getString(cursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.d(TAG, "getAllContacts" + phoneNumber);
                contactName.add(name);
                contactNumber.add(phoneNumber);

            }

            cursor.close();
            Intent in = new Intent(this, TrackLogic.class);
            in.putExtra("contact", contactName.toArray());
            Log.d(TAG, "Intent????" + contactName);
            in.putExtra("contact1", contactNumber.toArray());
        }

        class MyAdapter extends BaseAdapter implements
                CompoundButton.OnCheckedChangeListener {
            private SparseBooleanArray mCheckStates;
            LayoutInflater mInflater;
            TextView phoneView, contactView;
            CheckBox checkBox;

            MyAdapter() {
                mCheckStates = new SparseBooleanArray(contactName.size());
                mInflater = (LayoutInflater) contacts.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            }

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

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

            @Override
            public long getItemId(int position) {

                return 0;
            }

            @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                vi = convertView;
                if (convertView == null)
                    vi = mInflater.inflate(R.layout.row, null);
                contactView = (TextView) vi.findViewById(R.id.contact_name);
                phoneView = (TextView) vi.findViewById(R.id.phone_number);
                checkBox = (CheckBox) vi.findViewById(R.id.checkBox_id);
                contactView.setText(contactName.get(position));
                phoneView.setText(contactNumber.get(position));
                checkBox.setTag(position);
                checkBox.setChecked(mCheckStates.get(position, false));
                checkBox.setOnCheckedChangeListener(this);

                return vi;
            }

            public boolean isChecked(int position) {

                return mCheckStates.get(position, false);
            }

            public void setChecked(int position, boolean isChecked) {
                mCheckStates.put(position, isChecked);
                Log.d(TAG, "setChecked");
                notifyDataSetChanged();
            }

            public void toggle(int position) {
                setChecked(position, !isChecked(position));
            }

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {


                mCheckStates.put((Integer) buttonView.getTag(), isChecked);
            }
        }
    }

I cannot seem to figure it out and its a real drain on me now. Any help is highly appreciated.. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

update

check this link this is extactly what you want to do

try this in your buttons onclick method

SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
if (checkedItems != null) {
    for (int i=0; i<checkedItems.size(); i++) {
        if (checkedItems.valueAt(i)) {
            String item = listView.getAdapter().getItem(
                                  checkedItems.keyAt(i)).toString();
            Log.i(TAG,item + " was selected");
        }
    }
}

in your case you can also do this

for (int i = 0; i < myAdapter.getCount(); i++)
     {
        if (myAdapter.isChecked(i))
        {
             Toast.makeText(context,
              "Item at position " +i+" is Checked!!",
              Toast.LENGTH_SHORT).show();
         }
      }

try adding listener to your checkbox in your custom adapter see example here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...