Listview for multiple selection only showing checkboxes and no display displayed
我正在编写一个代码来显示多选列表(一个有复选框),当用户检查一个项目时我想从数据库表中检索用户的名称和id,将其显示在烤面包上。下面的代码:Listview for multiple selection only showing checkboxes and no display displayed
活动代码
public class Add_To_Circle extends Activity {
ListView lv;
ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_to_circle);
// Get listview
lv = (ListView)findViewById(R.id.list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
... //all the other code,and then in another class in postexecute there is adapter:
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
//pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
Add_To_Circle.this, productsList,
android.R.layout.simple_list_item_multiple_choice, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
lv.setAdapter(adapter);
}
});
}
}
代码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">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
输出:一个空白列表视图中只有复选框,作为attched形象。我究竟做错了什么?我都遵循相同的步骤教程网络上仍没有运气:((
可以使用具有一个TextView的和checkbox.Here是链接自定义适配器,
http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html
http://androidcocktail.blogspot.in/2012/04/adding-checkboxes-to-custom-listview-in.html
,如果你想用简单的数组适配器,
参考此链接,
Selecting multiple items in ListView
感谢
谢谢你这么多:)我试图,希望它能解决。 – user3347803 2014-10-27 13:43:29
不用客气!!如果有效,请接受我的回答。 – micky 2014-10-27 13:46:03
好,会尽量给出一个答案,但我不得不更改适配器的答案,所以这部分是未经测试。
让我们去自下而上的,这是对列表中的每个单一元素的XML布局:
view_adapter_item_checklist.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
使用CheckedTextView消除了需要处理的复选框选中/取消处理自己。
现在我的代码中的ListView被放置在一个LinearLayout中,因此是0dp的高度。重要的部分是选择模式:
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:choiceMode="multipleChoice"
android:drawSelectorOnTop="false" />
适配器真的很简单。它根据上面的checktext XML获取一个字符串和充气列表行的列表。唯一的任务是设置视图的文本,实际上是:
SimpleChecklistAdapter:
public class SimpleChecklistAdapter extends ArrayAdapter<String> {
private final Context context;
private final List<String> values;
public SimpleChecklistAdapter(Context context, List<String> values) {
super(context, R.layout.view_adapter_item_checklist, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.view_adapter_item_checklist,
parent, false);
CheckedTextView text = (CheckedTextView)rowView.findViewById(R.id.item);
text.setText(values.get(position));
return rowView;
}
}
所有有剩下的就是使用新的花式清单适配器,而不是SimpleAdapter在你的代码
我把ListView和CheckedTextView放在同一个布局中吗? – user3347803 2014-10-27 13:46:53
不,如果那不是很清楚..不,只是更新您现有的listview与'android:choiceMode =“multipleChoice”' – cYrixmorten 2014-10-27 14:47:27
你可以使用自定义的适配器,每个行都有一个textview和复选框 – micky 2014-10-27 13:03:42
有没有办法让这段代码运行?当我编写R.layout.list_item代替android.R.layout.simple_list_item_multiple_choice时,列表视图具有所有的值,但是没有复选框。你能告诉可能的原因吗? – user3347803 2014-10-27 13:15:45
thatsy我只是说,而不是使用简单的适配器,你可以使用自定义适配器,有一个textview和复选框 – micky 2014-10-27 13:18:49