【问题标题】:How to disable item from listview when he is clicked android单击android时如何从listview中禁用项目
【发布时间】:2014-10-14 20:31:44
【问题描述】:

我有对话框,在对话框中我显示了来自电话的联系人姓名和电子邮件(带有列表视图)。我希望用户从 listview 中选择一个联系人并将该联系人设置为 edittext,当用户再次单击该联系人时,该联系人将被禁用。

一般用户不能从列表视图中选择一个联系人两次。当我选择一些联系人并关闭对话框后,我的问题来了

我的 xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:background="@drawable/borderitem"
    android:orientation="vertical" >

     <CheckedTextView
       android:id="@+id/text1"
       android:layout_width="fill_parent"
       android:layout_height="?android:attr/listPreferredItemHeight"
       android:textColor="@android:color/holo_green_dark"
       android:gravity="center_vertical"
       android:checkMark="@drawable/apptheme_btn_check_holo_light"
       android:paddingLeft="6dip"
       android:text="asd"
       android:paddingRight="6dip"
       />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:textStyle="italic"
        android:typeface="sans" />

</LinearLayout>

.java 文件,

Button buttonPickContact = (Button) findViewById(R.id.contactact_us_btn);
        buttonPickContact.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                list.clear();
                populateList();
                listview.setAdapter(adapter);
                listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

                listview.setOnItemClickListener(new OnItemClickListener() {
                    @SuppressLint("ResourceAsColor") @Override

                    public void onItemClick(AdapterView<?> p_arg0, View p_arg1,
                            int p_arg2, long p_arg3) {


                        CheckedTextView checkText = (CheckedTextView) p_arg1
                                .findViewById(R.id.text1);
                        checkText.setChecked(!checkText.isChecked());
                        checkText.setPressed(true);
                    //Here i want to disable already picked contact




                    }

                });
            }
        });

private void populateList() {

        ContactsProvider cpro = new ContactsProvider(getApplicationContext());
        List<Contact> contacts = cpro.getContacts();
        for (Contact cnt : contacts) {
            // add all contacts in map
            HashMap<String, String> map = new HashMap<String, String>();
            // then put email and name of contacts in map
            map.put("name", cnt.name);
            map.put("email", cnt.email);
            list.add(map);
        }

        LayoutInflater inflater = getLayoutInflater();
        final View viewBox = inflater.inflate(R.layout.phone_listview, null);
        Button btn = (Button) viewBox.findViewById(R.id.mailsButton);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setEmails();
            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(ReferFriend.this);
        builder.setView(viewBox);
        // dialog, with him create a window ,in this window show name and email
        // of contacts
        dialog = builder.create();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        listview = (ListView) viewBox.findViewById(R.id.listt);
        dialog.show();
    }

【问题讨论】:

    标签: android listview android-listview android-contacts


    【解决方案1】:

    您需要设置已禁用列表项的位置的arrayList,并在每个项目上单击将其位置放入该数组,然后在您的适配器和 getView 方法中检查数组是否包含当前位置,然后禁用它。
    在你的 getView 方法中

    if(arrayList.contain(Object)position))
         //disable this row  
    

    并在onItem点击

     adapter.arrayList.put(position);
    

    【讨论】:

      猜你喜欢
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      相关资源
      最近更新 更多