Android的 - 在ListView的多选 [英] Android - Multiple selection in ListView
问题描述
好吧,我坚持整天试图让复选框列表视图中添加。我有两个部分。
- 如果我点击ListBox中的任何项目时,它会加载的东西(这部分已经实现)。
- 有每个项目的复选框。我希望能够检查所有10或任何数量的项目,这样即使我滚动列表,选择不会迷路。
任何人都可以请帮我有关吗?其中我查了解决的是这个解决方案,但我不要调整整个code,除非它是必要的。
MAINACTIVITY:
公共类ShowList延伸活动{ 静态最后弦乐LIST_KEY_ID =BOOKID;
静态最后弦乐LIST_KEY_NAME =BOOKNAME;
静态最后弦乐LIST_KEY_WRITER =作家; ListView控件列表;
MylibmanList适配器; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_show_list);
setupActionBar(); mylibmandbhandler DB =新mylibmandbhandler(本);
清单< mylibman> allfld = db.getAllRecord();
db.close();
ArrayList的<&HashMap的LT;字符串,字符串>> allbookList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
对于(mylibman CN:allfld){
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
map.put(LIST_KEY_ID,Integer.toString(cn.getBookid()));
map.put(LIST_KEY_NAME,cn.getBookname());
map.put(LIST_KEY_WRITER,cn.getWriter()); allbookList.add(地图);
}
名单=(ListView控件)findViewById(R.id.list);
适配器=新MylibmanList(这一点,allbookList);
list.setAdapter(适配器); list.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
batchid.clear();
HashMap的<字符串,字符串> list_hashMap =(HashMap的<字符串,字符串>)parent.getItemAtPosition(位置);
字符串currlist = list_hashMap.get(LIST_KEY_ID);
意图returnIntent =新的Intent();
returnIntent.putExtra(BOOKID,currlist);
的setResult(RESULT_OK,returnIntent);
完();
}
});
适配器类:
公共类MylibmanList延伸BaseAdapter {
私人活动活动;
私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
私有静态LayoutInflater吹气= NULL; 公共MylibmanList(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
活性=一个;
数据= D;
吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} 静态类ViewHolder {
TextView的称号;
TextView的艺术家;
TextView的持续时间;
复选框检查;
} 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看VI = convertView;
如果(convertView == NULL)
VI = inflater.inflate(R.layout.listrow_row,NULL); ViewHolder持有人=新ViewHolder();
holder.title =(TextView中)vi.findViewById(R.id.title);
holder.artist =(TextView中)vi.findViewById(R.id.artist);
holder.duration =(TextView中)vi.findViewById(R.id.duration);
holder.check =(复选框)vi.findViewById(R.id.check); holder.check.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
Log.d(测试,T3); //不知道该怎么办
}
});
vi.setTag(保持器);
holder.check.setTag(data.get(位置)); HashMap的<字符串,字符串>书=新的HashMap<字符串,字符串>();
本书= data.get(位置); ViewHolder holderfin =(ViewHolder)vi.getTag();
holderfin.title.setText(book.get(ShowList.LIST_KEY_NAME));
holderfin.artist.setText(book.get(ShowList.LIST_KEY_WRITER));
holderfin.duration.setText(book.get(ShowList.LIST_KEY_ID));
holderfin.check.setChecked(假);
返回VI;
}
}
XML:
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
机器人:填充=5dip> <的LinearLayout机器人:ID =@ + ID /缩略图
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:填充=3dip
机器人:layout_alignParentLeft =真
机器人:背景=@绘制/ image_bg
机器人:layout_marginRight =5dip>
<! -
< ImageView的
机器人:ID =@ + ID / list_image
机器人:contentDescription =@字符串/ bookimage
机器人:layout_width =50dip
机器人:layout_height =50dip/>
- >
< / LinearLayout中>
<的TextView
机器人:ID =@ + ID /标题
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignTop =@ + ID /缩略图
机器人:layout_toRightOf =@ + ID /缩略图
机器人:focusableInTouchMode =假
机器人:可点击=假
机器人:可聚焦=假
机器人:文字颜色=#040404
机器人:字体=SANS
机器人:TEXTSIZE =18sp
机器人:文字样式=大胆/>
<的TextView
机器人:ID =@ + ID /艺术家
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ ID /标题
机器人:focusableInTouchMode =假
机器人:可点击=假
机器人:可聚焦=假
机器人:文字颜色=#343434
机器人:TEXTSIZE =13SP
机器人:layout_marginTop =1dip
机器人:layout_toRightOf =@ + ID /缩略图/>
<的TextView
机器人:ID =@ + ID /时间
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_alignTop =@ ID /标题
机器人:重力=右
机器人:focusableInTouchMode =假
机器人:可点击=假
机器人:可聚焦=假
机器人:layout_marginRight =5dip
机器人:TEXTSIZE =12SP
机器人:文字颜色=#10bcc9
机器人:文字样式=大胆/> <复选框
机器人:ID =@ + ID /勾选
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_centerVertical =真
机器人:可聚焦=假
机器人:layout_marginRight =10dp/>
< / RelativeLayout的>
基本上,你可以只包含所选项目HashSet的。在code看起来像这样
公共类MylibmanList延伸BaseAdapter {
私人活动活动;
私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
私有静态LayoutInflater吹气= NULL; HashSet的<串GT; selectedBooks =新的HashSet<串GT;(); //这个监听器会在您所有的复选框被使用,有没有必要
//创建为每个复选框的监听器。
CompoundButton.OnCheckedChangeListener checkChangedListener =新CompoundButton.OnCheckedChangeListener {
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
字符串bookDuration =(字符串)buttonView.getTag();
如果(器isChecked){
selectedBooks.add(bookDuration);
}其他{
selectedBooks.remove(bookDuration);
}
}
}
公共MylibmanList(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
活性=一个;
数据= D;
吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} 静态类ViewHolder {
TextView的称号;
TextView的艺术家;
TextView的持续时间;
复选框检查;
}
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewHolder持有人;
如果(convertView == NULL){
convertView = inflater.inflate(R.layout.listrow_row,NULL); 持有人=新ViewHolder();
holder.title =(TextView中)convertView.findViewById(R.id.title);
holder.artist =(TextView中)convertView.findViewById(R.id.artist);
holder.duration =(TextView中)convertView.findViewById(R.id.duration);
holder.check =(复选框)convertView.findViewById(R.id.check);
holder.check.setOnCheckedChangeListener(checkChangedListener); convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
}
HashMap的<字符串,字符串>书=新的HashMap<字符串,字符串>();
本书=(HashMap的<字符串,字符串>)的getItem(位置); holder.check.setTag(book.get(ShowList.LIST_KEY_ID)); holder.title.setText(book.get(ShowList.LIST_KEY_NAME));
holder.artist.setText(book.get(ShowList.LIST_KEY_WRITER));
holder.duration.setText(book.get(ShowList.LIST_KEY_ID)); 布尔bookSelected = FALSE;
如果(selectedBooks.contains(book.get(ShowList.LIST_KEY_ID))){
bookSelected = TRUE;
} holder.check.setChecked(bookSelected); 返回convertView;
}
我已经改变了你的 getView
升技。该viewHolder现在将每个视图创建一次,(因为它应该)。另外,如果它不是一个太大的喧嚣,你应该为你的书类。类似
类Book {
字符串称号;
字符串的艺术家;
持续时间长;
}
Okay, I am stuck whole day trying to get Checkbox added in listview. I have two part
- If I click on any item in the listbox, it will load something (this part is already implemented).
- There is a checkbox with each item. I want to be able to check any 10 or whatever number of items so that even if I scroll through the list, the selection will not get lost.
Can anyone please help me regarding that? One of the solution I checked is this solution, but I don't want to restructure whole code unless it is necessary.
MAINACTIVITY:
public class ShowList extends Activity{
static final String LIST_KEY_ID = "bookid";
static final String LIST_KEY_NAME = "bookname";
static final String LIST_KEY_WRITER = "writer";
ListView list;
MylibmanList adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_list);
setupActionBar();
mylibmandbhandler db = new mylibmandbhandler(this);
List<mylibman> allfld = db.getAllRecord();
db.close();
ArrayList<HashMap<String, String>> allbookList = new ArrayList<HashMap<String, String>>();
for (mylibman cn : allfld) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(LIST_KEY_ID, Integer.toString(cn.getBookid()));
map.put(LIST_KEY_NAME, cn.getBookname());
map.put(LIST_KEY_WRITER, cn.getWriter());
allbookList.add(map);
}
list=(ListView)findViewById(R.id.list);
adapter=new MylibmanList(this, allbookList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
batchid.clear();
HashMap<String, String> list_hashMap = (HashMap<String, String>) parent.getItemAtPosition(position);
String currlist = list_hashMap.get(LIST_KEY_ID);
Intent returnIntent = new Intent();
returnIntent.putExtra("bookid",currlist);
setResult(RESULT_OK,returnIntent );
finish();
}
});
ADAPTER CLASS:
public class MylibmanList extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public MylibmanList(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
TextView title;
TextView artist;
TextView duration;
CheckBox check;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.listrow_row, null);
ViewHolder holder = new ViewHolder();
holder.title = (TextView)vi.findViewById(R.id.title);
holder.artist = (TextView)vi.findViewById(R.id.artist);
holder.duration = (TextView)vi.findViewById(R.id.duration);
holder.check = (CheckBox)vi.findViewById(R.id.check);
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("test","t3"); // Don't know what to do
}
});
vi.setTag(holder);
holder.check.setTag(data.get(position));
HashMap<String, String> book = new HashMap<String, String>();
book = data.get(position);
ViewHolder holderfin = (ViewHolder) vi.getTag();
holderfin.title.setText(book.get(ShowList.LIST_KEY_NAME));
holderfin.artist.setText(book.get(ShowList.LIST_KEY_WRITER));
holderfin.duration.setText(book.get(ShowList.LIST_KEY_ID));
holderfin.check.setChecked(false);
return vi;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<!--
<ImageView
android:id="@+id/list_image"
android:contentDescription="@string/bookimage"
android:layout_width="50dip"
android:layout_height="50dip"/>
-->
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textColor="#040404"
android:typeface="sans"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:textColor="#343434"
android:textSize="13sp"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail" />
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/title"
android:gravity="right"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:layout_marginRight="5dip"
android:textSize="12sp"
android:textColor="#10bcc9"
android:textStyle="bold"/>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:layout_marginRight="10dp" />
</RelativeLayout>
Basically, you can have a hashset that contains only the selected items. The code would look like this
public class MylibmanList extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
HashSet<String> selectedBooks = new HashSet<String>();
//This listener will be used on all your checkboxes, there's no need to
//create a listener for every checkbox.
CompoundButton.OnCheckedChangeListener checkChangedListener = new CompoundButton.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String bookDuration = (String) buttonView.getTag();
if(isChecked){
selectedBooks.add(bookDuration);
}else{
selectedBooks.remove(bookDuration);
}
}
}
public MylibmanList(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
TextView title;
TextView artist;
TextView duration;
CheckBox check;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = inflater.inflate(R.layout.listrow_row, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.artist = (TextView) convertView.findViewById(R.id.artist);
holder.duration = (TextView) convertView.findViewById(R.id.duration);
holder.check = (CheckBox) convertView.findViewById(R.id.check);
holder.check.setOnCheckedChangeListener(checkChangedListener);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> book = new HashMap<String, String>();
book = (HashMap<String, String>) getItem(position);
holder.check.setTag(book.get(ShowList.LIST_KEY_ID));
holder.title.setText(book.get(ShowList.LIST_KEY_NAME));
holder.artist.setText(book.get(ShowList.LIST_KEY_WRITER));
holder.duration.setText(book.get(ShowList.LIST_KEY_ID));
boolean bookSelected = false;
if(selectedBooks.contains(book.get(ShowList.LIST_KEY_ID))){
bookSelected = true;
}
holder.check.setChecked(bookSelected);
return convertView;
}
I've changed your getView
abit. The viewHolder will now be created only once for each view (as it should). Also, if its not too much of a hustle, you should create a class for your book. Something like
Class Book{
String title;
String artist;
Long duration;
}
这篇关于Android的 - 在ListView的多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!