复选框列表视图中为多选联系人视图、中为、多选、复选框

2023-09-12 08:01:23 作者:琉璃.

我曾试图通过布局充气放复选框ListView和我得到了成功,但问题是,当我选择多个联系人是没有问题的,但是当我取消它与放大器;当我向下滚动和放大器;再回到那个复选框取消其获得自动选择...

 公共类Contactlist_selfActivity扩展ListActivity {
/ **第一次创建活动时调用。 * /

私人的ArrayList<联系> CONTACT_LIST = NULL;
私人ProgressDialog mProgressDialog = NULL;
私人contactAdapter mContactAdapter = NULL;
私人可运行mViewcontacts = NULL;
私人SparseBooleanArray mSelectedContacts =新SparseBooleanArray();
私人的ArrayList<联系>项目;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    CONTACT_LIST =新的ArrayList<联系>();
    this.mContactAdapter =新contactAdapter(这一点,R.layout.listview,
            联系人列表);
    ListView的LV = getListView();
    setListAdapter(this.mContactAdapter);
    lv.setItemsCanFocus(假);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    //}
    mViewcontacts =新的Runnable(){

        @覆盖
        公共无效的run(){
            // TODO自动生成方法存根
            getContacts();
        }
    };

    线程线程=新主题(空,mViewcontacts,ContactReadBackground);
    thread.start();
    mProgressDialog = ProgressDialog.show(Contactlist_selfActivity.this,
            请等待......,Retriving与我们联系......,真正的);
}

@燮pressWarnings(未使用)
私人无效getContacts(){
    // TODO自动生成方法存根

    尝试 {

        的String []投影=新的String [] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER,
                ContactsContract.Contacts._ID};

        光标mCursor = managedQuery(
                ContactsContract.Contacts.CONTENT_URI,投影,
                ContactsContract.Contacts.HAS_PHONE_NUMBER +=?,
                新的String [] {1},
                ContactsContract.Contacts.DISPLAY_NAME);

        而(mCursor.moveToNext()){
            洽洽=新联系人();

            字符串的ContactID = mCursor.getString(mCursor
                    .getColumnIndex(ContactsContract.Contacts._ID));

            contact.setContactName(mCursor.getString(mCursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            contact_list.add(接触);
        }
        mCursor.close();

        runOnUiThread(returnRes);

    }赶上(例外五){
        // TODO:处理异常
        Log.d(getContacts,e.getMessage());
    }
}

公共类contactAdapter扩展ArrayAdapter<联系> {
    私人INT []器isChecked;

    公共contactAdapter(上下文的背景下,INT textViewResourceId,
            ArrayList的<联系> items1){
        超(背景下,textViewResourceId,items1);
        项目= items1;
    }

    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,
            ViewGroup中父){
        // TODO自动生成方法存根
        最终诠释position_clicked = 0;
        // Log.i(房间隔缺损,getView:+的getItem(位置));

        如果(convertView == NULL){

            LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.listview,父母,假);
        }

        联系方式联系人= items.get(位置);
        =器isChecked新INT [items.size()];

        如果(联系人!= NULL){
            最终的复选框nameCheckBox =(复选框)convertView
                    .findViewById(R.id.checkBox);
            nameCheckBox.setChecked(mSelectedContacts.get(位置));

            的for(int i = 0; I< isChecked.length;我++){

            }

            如果(nameCheckBox!= NULL){
                nameCheckBox.setText(contacts.getContactName());
            }

            nameCheckBox.setOnClickListener(新OnClickListener(){

                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根
                    器isChecked [position_clicked] =位置;
                    Log.d(位置,将String.valueOf(位置));
                }
            });

        }

        返回convertView;
    }
}

私人可运行returnRes =新的Runnable(){

    @覆盖
    公共无效的run(){
        // TODO自动生成方法存根
        如果(mProgressDialog.isShowing())
            mProgressDialog.dismiss();
        mContactAdapter.notifyDataSetChanged();
    }
};
}
 

解决方案

我找到了答案.... 我刚刚采取了一个新的变量接触类...

 公共类PlanetsActivity延伸活动{
私人的ListView mainListView;
私人联系[] contact_read;
私人光标mCursor;
私人ArrayAdapter<联系与GT; listAdapter;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    //找到的ListView资源。
    mainListView =(ListView控件)findViewById(R.id.mainListView);

    mainListView
            .setOnItemClickListener(新AdapterView.OnItemClickListener(){
                @覆盖
                公共无效onItemClick(适配器视图<>父,查看项目,
                        INT位置,长的id){
                    联系星球= listAdapter.getItem(位置);
                    planet.toggleChecked();
                    ContactViewHolder viewHolder =(ContactViewHolder)项目
                            .getTag();
                    viewHolder.getCheckBox()setChecked(planet.isChecked())。
                }
            });

    //引发查询并获取联系人。

    的String []投影=新的String [] {Contacts.HAS_PHONE_NUMBER,
            Contacts._ID,Contacts.DISPLAY_NAME};

    mCursor = managedQuery(Contacts.CONTENT_URI,投影,
            Contacts.HAS_PHONE_NUMBER +=?,新的String [] {1},
            Contacts.DISPLAY_NAME);

    如果(mCursor!= NULL){
        mCursor.moveToFirst();
        contact_read =新的联系[mCursor.getCount()];

        //添加联系人到Array

        INT J = 0;
        做 {

            contact_read [J] =新的联系方式(mCursor.getString(mCursor
                    .getColumnIndex(Contacts.DISPLAY_NAME)));
            J ++;
        }而(mCursor.moveToNext());

    } 其他 {
        的System.out.println(光标为NULL);
    }

    //添加联系人级到ArrayList

    ArrayList的<联系与GT; planetList =新的ArrayList<联系与GT;();
    planetList.addAll(Arrays.asList(contact_read));

    //设置我们的自定义数组适配器作为ListView的适配器。
    listAdapter =新ContactArrayAdapter(这一点,planetList);
    mainListView.setAdapter(listAdapter);
}

/ **保存联系人数据。 * /
@燮pressWarnings(未使用)
私有静态类联系{
    私人字符串名称=;
    私人布尔查= FALSE;

    公共联系(){
    }

    公共联系(字符串名称){
        this.name =名称;
    }

    公共联系(字符串名称,布尔检查){
        this.name =名称;
        this.checked =检查;
    }

    公共字符串的getName(){
        返回名称;
    }

    公共无效setname可以(字符串名称){
        this.name =名称;
    }

    公共布尔器isChecked(){
        返回检查;
    }

    公共无效setChecked(布尔检查){
        this.checked =检查;
    }

    公共字符串的toString(){
        返回名称;
    }

    公共无效toggleChecked(){
        !检查=检查;
    }
}

/ **存放子视图一行。 * /
@燮pressWarnings(未使用)
私有静态类ContactViewHolder {
    私人复选框复选框;
    私人的TextView TextView的;

    公共ContactViewHolder(){
    }

    公共ContactViewHolder(TextView的TextView的,复选框复选框){
        this.checkBox =复选框;
        this.textView = TextView的;
    }

    公开复选框getCheckBox(){
        返回复选框;
    }

    公共无效setCheckBox(复选框复选框){
        this.checkBox =复选框;
    }

    公众的TextView getTextView(){
        返回的TextView;
    }

    公共无效setTextView(TextView中的TextView){
        this.textView = TextView的;
    }
}

/ **定义适配器,用于显示联系对象的数组。 * /
私有静态类ContactArrayAdapter扩展ArrayAdapter<联系与GT; {

    私人LayoutInflater充气;

    公共ContactArrayAdapter(上下文的背景下,列表与LT;联系与GT; planetList){
        超(背景下,R.layout.simplerow,R.id.rowTextView,planetList);
        //缓存的LayoutInflate避免询问,每次一个新的。
        充气= LayoutInflater.from(上下文);
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        //联系,以显示
        联系星球=(联系)this.getItem(位置);
        的System.out.println(将String.valueOf(位置));

        //每行的子视图。
        复选框复选框;
        的TextView TextView的;

        //创建一个新的行视图
        如果(convertView == NULL){
            convertView = inflater.inflate(R.layout.simplerow,NULL);

            //找到孩子的意见。
            TextView的=(的TextView)convertView
                    .findViewById(R.id.rowTextView);
            复选框=(复选框)convertView.findViewById(R.id.C​​heckBox01);

            //优化:标记的行与它的孩子的意见,所以我们不
            // 不得不
            //调用findViewById()后,当我们再次使用该行。
            convertView.setTag(新ContactViewHolder(TextView的,多选));

            //如果复选框被触发,更新联系人被打上了。
            checkBox.setOnClickListener(新View.OnClickListener(){
                公共无效的onClick(视图v){
                    复选框CB =(复选框)V;
                    联系方式联系方式=(联系)cb.getTag();
                    contact.setChecked(cb.isChecked());
                }
            });
        }
        //重用现有行观点
        其他 {
            //因为我们使用ViewHolder,我们避免调用
            // findViewById()。
            ContactViewHolder viewHolder =(ContactViewHolder)convertView
                    .getTag();
            复选框= viewHolder.getCheckBox();
            的TextView = viewHolder.getTextView();
        }

        //标签的复选框的接触中,展示,让大家
        // 能够
        //获取联系人中的onClick()时,该复选框被触发。
        checkBox.setTag(行星);

        //显示联系人数据
        checkBox.setChecked(planet.isChecked());
        textView.setText(planet.getName());

        返回convertView;
    }

}

公共对象onRetainNonConfigurationInstance(){
    返回contact_read;
}
 

}

access 窗体 如何利用复选框多选,将数据录入到表中的一个字段中 多值字段

I had tried to to put checkbox in listview through layout inflator and I got success but the problem is when I select the multiple contacts there is no problem but when I deselect it & when I scroll down & then go back to that deselected checkbox its get automatically selected...

public class Contactlist_selfActivity extends ListActivity {
/** Called when the activity is first created. */

private ArrayList<contact> contact_list = null;
private ProgressDialog mProgressDialog = null;
private contactAdapter mContactAdapter = null;
private Runnable mViewcontacts = null;
private SparseBooleanArray mSelectedContacts = new SparseBooleanArray();
private ArrayList<contact> items;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    contact_list = new ArrayList<contact>();
    this.mContactAdapter = new contactAdapter(this, R.layout.listview,
            contact_list);
    ListView lv = getListView();
    setListAdapter(this.mContactAdapter);
    lv.setItemsCanFocus(false);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    // }
    mViewcontacts = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            getContacts();
        }
    };

    Thread thread = new Thread(null, mViewcontacts, "ContactReadBackground");
    thread.start();
    mProgressDialog = ProgressDialog.show(Contactlist_selfActivity.this,
            "Please Wait...", "Retriving Contacts...", true);
}

@SuppressWarnings("unused")
private void getContacts() {
    // TODO Auto-generated method stub

    try {

        String[] projection = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER,
                ContactsContract.Contacts._ID };

        Cursor mCursor = managedQuery(
                ContactsContract.Contacts.CONTENT_URI, projection,
                ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?",
                new String[] { "1" },
                ContactsContract.Contacts.DISPLAY_NAME);

        while (mCursor.moveToNext()) {
            contact contact = new contact();

            String contactId = mCursor.getString(mCursor
                    .getColumnIndex(ContactsContract.Contacts._ID));

            contact.setContactName(mCursor.getString(mCursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            contact_list.add(contact);
        }
        mCursor.close();

        runOnUiThread(returnRes);

    } catch (Exception e) {
        // TODO: handle exception
        Log.d("getContacts", e.getMessage());
    }
}

public class contactAdapter extends ArrayAdapter<contact> {
    private int[] isChecked;

    public contactAdapter(Context context, int textViewResourceId,
            ArrayList<contact> items1) {
        super(context, textViewResourceId, items1);
        items = items1;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub
        final int position_clicked = 0;
        // Log.i("asd", "getView :" + getItem(position));

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.listview, parent, false);
        }

        contact contacts = items.get(position);
        isChecked = new int[items.size()];

        if (contacts != null) {
            final CheckBox nameCheckBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox);
            nameCheckBox.setChecked(mSelectedContacts.get(position));

            for (int i = 0; i < isChecked.length; i++) {

            }

            if (nameCheckBox != null) {
                nameCheckBox.setText(contacts.getContactName());
            }

            nameCheckBox.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    isChecked[position_clicked] = position;
                    Log.d("position", String.valueOf(position));
                }
            });

        }

        return convertView;
    }
}

private Runnable returnRes = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        if (mProgressDialog.isShowing())
            mProgressDialog.dismiss();
        mContactAdapter.notifyDataSetChanged();
    }
};
}

解决方案

i found the answer.... i had just taken a new variable in contact class...

public class PlanetsActivity extends Activity {
private ListView mainListView;
private Contact[] contact_read;
private Cursor mCursor;
private ArrayAdapter<Contact> listAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Find the ListView resource.
    mainListView = (ListView) findViewById(R.id.mainListView);

    mainListView
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View item,
                        int position, long id) {
                    Contact planet = listAdapter.getItem(position);
                    planet.toggleChecked();
                    ContactViewHolder viewHolder = (ContactViewHolder) item
                            .getTag();
                    viewHolder.getCheckBox().setChecked(planet.isChecked());
                }
            });

    // Throw Query and fetch the contacts.

    String[] projection = new String[] { Contacts.HAS_PHONE_NUMBER,
            Contacts._ID, Contacts.DISPLAY_NAME };

    mCursor = managedQuery(Contacts.CONTENT_URI, projection,
            Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
            Contacts.DISPLAY_NAME);

    if (mCursor != null) {
        mCursor.moveToFirst();
        contact_read = new Contact[mCursor.getCount()];

        // Add Contacts to the Array

        int j = 0;
        do {

            contact_read[j] = new Contact(mCursor.getString(mCursor
                    .getColumnIndex(Contacts.DISPLAY_NAME)));
            j++;
        } while (mCursor.moveToNext());

    } else {
        System.out.println("Cursor is NULL");
    }

    // Add Contact Class to the Arraylist

    ArrayList<Contact> planetList = new ArrayList<Contact>();
    planetList.addAll(Arrays.asList(contact_read));

    // Set our custom array adapter as the ListView's adapter.
    listAdapter = new ContactArrayAdapter(this, planetList);
    mainListView.setAdapter(listAdapter);
}

/** Holds Contact data. */
@SuppressWarnings("unused")
private static class Contact {
    private String name = "";
    private boolean checked = false;

    public Contact() {
    }

    public Contact(String name) {
        this.name = name;
    }

    public Contact(String name, boolean checked) {
        this.name = name;
        this.checked = checked;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return checked;
    }

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

    public String toString() {
        return name;
    }

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

/** Holds child views for one row. */
@SuppressWarnings("unused")
private static class ContactViewHolder {
    private CheckBox checkBox;
    private TextView textView;

    public ContactViewHolder() {
    }

    public ContactViewHolder(TextView textView, CheckBox checkBox) {
        this.checkBox = checkBox;
        this.textView = textView;
    }

    public CheckBox getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(CheckBox checkBox) {
        this.checkBox = checkBox;
    }

    public TextView getTextView() {
        return textView;
    }

    public void setTextView(TextView textView) {
        this.textView = textView;
    }
}

/** Custom adapter for displaying an array of Contact objects. */
private static class ContactArrayAdapter extends ArrayAdapter<Contact> {

    private LayoutInflater inflater;

    public ContactArrayAdapter(Context context, List<Contact> planetList) {
        super(context, R.layout.simplerow, R.id.rowTextView, planetList);
        // Cache the LayoutInflate to avoid asking for a new one each time.
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Contact to display
        Contact planet = (Contact) this.getItem(position);
        System.out.println(String.valueOf(position));

        // The child views in each row.
        CheckBox checkBox;
        TextView textView;

        // Create a new row view
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.simplerow, null);

            // Find the child views.
            textView = (TextView) convertView
                    .findViewById(R.id.rowTextView);
            checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);

            // Optimization: Tag the row with it's child views, so we don't
            // have to
            // call findViewById() later when we reuse the row.
            convertView.setTag(new ContactViewHolder(textView, checkBox));

            // If CheckBox is toggled, update the Contact it is tagged with.
            checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    Contact contact = (Contact) cb.getTag();
                    contact.setChecked(cb.isChecked());
                }
            });
        }
        // Reuse existing row view
        else {
            // Because we use a ViewHolder, we avoid having to call
            // findViewById().
            ContactViewHolder viewHolder = (ContactViewHolder) convertView
                    .getTag();
            checkBox = viewHolder.getCheckBox();
            textView = viewHolder.getTextView();
        }

        // Tag the CheckBox with the Contact it is displaying, so that we
        // can
        // access the Contact in onClick() when the CheckBox is toggled.
        checkBox.setTag(planet);

        // Display Contact data
        checkBox.setChecked(planet.isChecked());
        textView.setText(planet.getName());

        return convertView;
    }

}

public Object onRetainNonConfigurationInstance() {
    return contact_read;
}

}