[英]How to make a custom listView checkable?
My problem is that if I press one of my list items, the checkbox is just checked during the time my finger touch it. 我的问题是,如果我按了我的列表项之一,则在我的手指触摸该复选框时,该复选框就会被选中。 I've been searching like crazy, but I have a hard time to believe that there is no simple fix to this. 我一直在疯狂搜索,但是我很难相信对此没有简单的解决方法。
Here is a sample of my code. 这是我的代码示例。
I am using the following XML for my list items: 我将以下XML用于列表项:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#000000"
android:textSize="25dp"
/>
Attached to this class, it is being used in my array adapter. 附加到此类,正在我的阵列适配器中使用。
public class Misshandel extends Activity {
Button misstankt;
ListView mListView;
ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.forhor);
misstankt = (Button) findViewById(R.id.misstanktKnapp);
mListView = (ListView) findViewById(R.id.forhorsLista);
final String[] misshandelMT = getResources().getStringArray(R.array.misshandelMT);
misstankt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(Misshandel.this, R.layout.custom_list_item, misshandelMT);
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
});
}
}
So to sum it up, what I want to achive is to be able to check my boxes! 综上所述,我想实现的目标是能够选中我的盒子!
Here is the full class if you want to check it out: http://codepaste.net/fg3xiz 如果您想签出完整的课程,请参阅以下完整课程: http : //codepaste.net/fg3xiz
Thanks for your time! 谢谢你的时间!
You should post more detailed information, but I'll give you my code for dialog in which you can choose several options: 您应该发布更多详细信息,但是我将为您提供对话框代码,您可以在其中选择几个选项:
`multiselect.xml' `multiselect.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:orientation="vertical"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<ListView
android:id="@+id/values"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:choiceMode="multipleChoice" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/selectAll"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="@string/select_all" />
<Button
android:id="@+id/ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/ok" />
<Button
android:id="@+id/deselectAll"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/select_none" />
</LinearLayout>
</LinearLayout>
And the Dialog class (with support for translation bonus): 和Dialog类(支持翻译奖励):
public class MultiSelectDialog extends Dialog {
private final String title;
private ListView items;
private List<String> selectedItems;
private HashMap<String, String> translation;
private List<String> displayedValues;
public MultiSelectDialog(Context context, String title, List<String> values,
HashMap<String, String> translation) {
super(context);
this.title = title;
this.translation = translation;
if(translation != null) {
displayedValues = FUtils.collect(translation.entrySet().iterator(),
new Converter<HashMap.Entry<String, String>, String>() {
public String convert(Entry<String, String> object) {
return object.getValue();
}
});
}
else {
displayedValues = values;
}
selectedItems = new LinkedList<String>();
}
public List<String> getSelectedItems() {
return selectedItems;
}
public void setSelectedItems(List<String> selectedItems) {
this.selectedItems.clear();
this.selectedItems.addAll(selectedItems);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multiselect);
setTitle(title);
items = (ListView) findViewById(R.id.values);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_list_item_multiple_choice, displayedValues);
items.setAdapter(adapter);
for(int i = 0; i < adapter.getCount(); ++i) {
items.setItemChecked(i, false);
}
if(selectedItems != null) {
for(int i = 0; i < selectedItems.size(); ++i) {
String value = getTranslatedValue(selectedItems.get(i));
for(int j = 0; j < items.getAdapter().getCount(); ++j) {
if(items.getAdapter().getItem(j).equals(value)) {
items.setItemChecked(j, true);
}
}
}
}
Button selectAll = (Button) findViewById(R.id.selectAll);
selectAll.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < adapter.getCount(); ++i) {
items.setItemChecked(i, true);
}
}
});
Button deselectAll = (Button) findViewById(R.id.deselectAll);
deselectAll.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < adapter.getCount(); ++i) {
items.setItemChecked(i, false);
}
}
});
Button okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
selectedItems.clear();
for(int i = 0; i < adapter.getCount(); ++i) {
if(items.isItemChecked(i)) {
String item = (String) items.getItemAtPosition(i);
reverseTranslateAndAdd(item);
}
}
dismiss();
}
});
}
private String getTranslatedValue(String key) {
if(translation == null) {
return key;
}
return translation.get(key);
}
private void reverseTranslateAndAdd(String item) {
if(translation != null) {
for(Entry<String, String> e : translation.entrySet()) {
String value = e.getValue();
if(value.equals(item)) {
selectedItems.add(e.getKey());
break;
}
}
}
else {
selectedItems.add(item);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.