[英]How to disable item from listview when he is clicked android
我有一個對話框,在對話框中,我顯示電話中的聯系人姓名和電子郵件(帶有列表視圖)。 我希望用戶從列表視圖中選擇一個聯系人,並將該聯系人設置為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();
}
您需要設置禁用的listItem位置的arrayList,並在每個項目上單擊以將其位置放置到此數組中,然后稍后在適配器和getView方法中可以檢查數組是否包含當前位置,然后將其禁用。
在您的getView方法中
if(arrayList.contain(Object)position))
//disable this row
然后在onItem中單擊
adapter.arrayList.put(position);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.