android– 如何获取自定义视图列表中的已检查行

android– 如何获取自定义视图列表中的已检查行,第1张

概述任何人都可以告诉我如何在自定义视图列表中获取已检查行的值?例如,我的代码有一个自定义视图列表,其中包含两个文本视图和一个复选框每当用户检查一行并点击提交按钮时,我需要检索两个textview中的信息.代码是publicclasscontactsextendsActivityimplementsOnItemClickLis

任何人都可以告诉我如何在自定义视图列表中获取已检查行的值?例如,我的代码有一个自定义视图列表,其中包含两个文本视图和一个复选框每当用户检查一行并点击提交按钮时,我需要检索两个textvIEw中的信息.

代码是

 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("\n");                        } 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);            }        }    }

我似乎无法弄明白,现在它真正消耗在我身上.任何帮助都非常感谢..
谢谢.

解决方法:

https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

您的自定义适配器必须实现Compoundbutton.OnCheckedchangelistener.使用SparseBooleanArray获取列表中的选中项.

然后

     cb.setChecked(mCheckStates.get(position, false));      cb.setonCheckedchangelistener(this);

然后使用选中状态将文本设置为复选框

      public boolean isChecked(int position) {        return mCheckStates.get(position, false);    }    public voID setChecked(int position, boolean isChecked) {        mCheckStates.put(position, isChecked);    }    public voID toggle(int position) {        setChecked(position, !isChecked(position));    }@OverrIDepublic voID onCheckedChanged(Compoundbutton buttonVIEw,        boolean isChecked) {    // Todo auto-generated method stub    if(isChecked)    {    buttonVIEw.setText("Hello");    }    else    {        buttonVIEw.setText("");    }     mCheckStates.put((Integer) buttonVIEw.getTag(), isChecked);    }

示例:

public class MainActivity extends Activity implements OnItemClickListener{List<String> name1 = new ArrayList<String>();List<String> phno1 = new ArrayList<String>();MyAdapter ma ;button select@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    getAllCallLogs(this.getContentResolver());    ListVIEw lv= (ListVIEw) findVIEwByID(R.ID.lv);     ma = new MyAdapter();    lv.setAdapter(ma);    lv.setonItemClickListener(this);     lv.setItemsCanFocus(false);    lv.setTextFilterEnabled(true);    // adding    select = (button) findVIEwByID(R.ID.button1);    select.setonClickListener(new OnClickListener()    {        @OverrIDe        public voID onClick(VIEw v) {              StringBuilder checkedcontacts= new StringBuilder();            System.out.println(".............."+ma.mCheckStates.size());            for(int i = 0; i < name1.size(); i++)                {                if(ma.mCheckStates.get(i)==true)                {                     checkedcontacts.append(name1.get(i).toString());                     checkedcontacts.append("\n");                }                else                {                    System.out.println("..Not Checked......"+name1.get(i).toString());                }            }                  Toast.MakeText(MainActivity.this,checkedcontacts,1000).show();        }           });}@OverrIDepublic voID onItemClick(AdapterVIEw<?> arg0, VIEw arg1, int arg2, long arg3) {    // Todo auto-generated method stub     ma.toggle(arg2);}public  voID getAllCallLogs(ContentResolver cr) {    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);    while (phones.movetoNext())    {      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.disPLAY_name));      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));      System.out.println(".................."+phoneNumber);       name1.add(name);      phno1.add(phoneNumber);    }    phones.close(); }class MyAdapter extends BaseAdapter implements Compoundbutton.OnCheckedchangelistener{  private SparseBooleanArray mCheckStates;   LayoutInflater mInflater;    TextVIEw tv1,tv;    CheckBox cb;    MyAdapter()    {        mCheckStates = new SparseBooleanArray(name1.size());        mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    }    @OverrIDe    public int getCount() {        // Todo auto-generated method stub        return name1.size();    }    @OverrIDe    public Object getItem(int position) {        // Todo auto-generated method stub        return position;    }    @OverrIDe    public long getItemID(int position) {        // Todo auto-generated method stub        return 0;    }    @OverrIDe    public VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) {        // Todo auto-generated method stub     VIEw vi=convertVIEw;         if(convertVIEw==null)         vi = mInflater.inflate(R.layout.row, null);          TextVIEw tv= (TextVIEw) vi.findVIEwByID(R.ID.textVIEw1);         tv1= (TextVIEw) vi.findVIEwByID(R.ID.textVIEw2);         cb = (CheckBox) vi.findVIEwByID(R.ID.checkBox1);         tv.setText("name :"+ name1.get(position));         tv1.setText("Phone No :"+ phno1.get(position));         cb.setTag(position);         cb.setChecked(mCheckStates.get(position, false));         cb.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);            System.out.println("hello...........");            notifyDataSetChanged();        }        public voID toggle(int position) {            setChecked(position, !isChecked(position));        }    @OverrIDe    public voID onCheckedChanged(Compoundbutton buttonVIEw,            boolean isChecked) {        // Todo auto-generated method stub         mCheckStates.put((Integer) buttonVIEw.getTag(), isChecked);        }} }

activity_main.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:layout_wIDth="match_parent"       androID:layout_height="wrap_content"       androID:layout_above="@+ID/button1"       androID:ID="@+ID/lv"/>     <button         androID:ID="@+ID/button1"         androID:layout_wIDth="wrap_content"         androID:layout_height="wrap_content"         androID:layout_alignParentBottom="true"         androID:layout_centerHorizontal="true"         androID:text="Select" /></relativeLayout>

row.xml

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" >    <TextVIEw        androID:ID="@+ID/textVIEw1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignParentleft="true"        androID:layout_alignParenttop="true"        androID:layout_marginleft="15dp"        androID:layout_margintop="34dp"        androID:text="TextVIEw" />    <TextVIEw        androID:ID="@+ID/textVIEw2"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignBottom="@+ID/checkBox1"        androID:layout_alignleft="@+ID/textVIEw1"        androID:text="TextVIEw" />    <CheckBox        androID:ID="@+ID/checkBox1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignParentRight="true"        androID:layout_below="@+ID/textVIEw1"        androID:layout_marginRight="22dp"        androID:layout_margintop="23dp" /></relativeLayout>

您可以勾选复选框并单击底部的按钮,选中的项目将以祝酒方式显示.

根据您的要求修改上述内容.

总结

以上是内存溢出为你收集整理的android – 如何获取自定义视图列表中的已检查行全部内容,希望文章能够帮你解决android – 如何获取自定义视图列表中的已检查行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1101826.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存