在Listview中使用复选框获取选定的项目

我正在创建一个Android应用程序,我有一个列表视图,显示我的手机中安装的所有应用程序

我的Listview是自定义的,它包含了一个Icon,Textview和Checkbox,使用这个图标是为了显示应用程序的图标,Textview是显示应用程序的名称,使用复选框是确定什么Item我选择的Listview,

我怎样才能确定什么是我在我的应用程序中点击按钮时在列表视图行中选择的复选框? 我是Android新手,所以我不知道我应该怎么做

这里是我的代码

public class AppInfo { public Drawable icon; public String applicationName; public AppInfo(){ super(); } public AppInfo(Drawable icon, String applicationName){ super(); this.icon = icon; this.applicationName = applicationName; } } 

 import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import android.app.Activity; import android.widget.CheckBox; public class AppInfoAdapter extends ArrayAdapter<AppInfo> { Context context; int layoutResourceId; AppInfo data[] = null; public AppInfoAdapter(Context context, int layoutResourceId, AppInfo[] data){ super(context, layoutResourceId,data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Override public View getView(int position, View convertView, ViewGroup parent){ View row = convertView; AppInfoHolder holder= null; if (row == null){ LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new AppInfoHolder(); holder.imgIcon = (ImageView) row.findViewById(R.id.imgPackageIcon); holder.txtTitle = (TextView) row.findViewById(R.id.txtApplicationName); holder.chkSelect = (CheckBox) row.findViewById(R.id.chkSelect); row.setTag(holder); } else{ holder = (AppInfoHolder)row.getTag(); } AppInfo appinfo = data[position]; holder.txtTitle.setText(appinfo.applicationName); holder.imgIcon.setImageDrawable(appinfo.icon); holder.chkSelect.setChecked(true); return row; } } 

 import android.widget.ImageView; import android.widget.TextView; import android.widget.CheckBox; public class AppInfoHolder { ImageView imgIcon; TextView txtTitle; CheckBox chkSelect; } 

 import android.app.Activity; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import android.content.pm.PackageInfo; public class CacheActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_cache); final ListView listApplication = (ListView)findViewById(R.id.listApplication); ApplicationInfo applicationInfo = getApplicationInfo(); PackageManager pm = getPackageManager(); List<PackageInfo> pInfo = new ArrayList<PackageInfo>(); pInfo.addAll(pm.getInstalledPackages(0)); AppInfo app_info[] = new AppInfo[pInfo.size()]; int counter = 0; for(PackageInfo item: pInfo){ try{ applicationInfo = pm.getApplicationInfo(item.packageName, 1); app_info[counter] = new AppInfo(pm.getApplicationIcon(applicationInfo), String.valueOf(pm.getApplicationLabel(applicationInfo))); System.out.println(counter); } catch(Exception e){ System.out.println(e.getMessage()); } counter++; } AppInfoAdapter adapter = new AppInfoAdapter(this, R.layout.listview_item_row, app_info); listApplication.setAdapter(adapter); } } 

假设你想获得点击一个按钮的复选框的行的项目。 假设根据您的标题“当我点击一个按钮时使用列表视图中的复选框获取选定的项目”。

试试下面。 只做如下更改。 保持其余的一样。

关于话题的解释和讨论@

https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

MainActivity.java

 public class MainActivity extends Activity { AppInfoAdapter adapter ; AppInfo app_info[] ; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView listApplication = (ListView)findViewById(R.id.listApplication); Button b= (Button) findViewById(R.id.button1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub StringBuilder result = new StringBuilder(); for(int i=0;i<adapter.mCheckStates.size();i++) { if(adapter.mCheckStates.get(i)==true) { result.append(app_info[i].applicationName); result.append("\n"); } } Toast.makeText(MainActivity.this, result, 1000).show(); } }); ApplicationInfo applicationInfo = getApplicationInfo(); PackageManager pm = getPackageManager(); List<PackageInfo> pInfo = new ArrayList<PackageInfo>(); pInfo.addAll(pm.getInstalledPackages(0)); app_info = new AppInfo[pInfo.size()]; int counter = 0; for(PackageInfo item: pInfo){ try{ applicationInfo = pm.getApplicationInfo(item.packageName, 1); app_info[counter] = new AppInfo(pm.getApplicationIcon(applicationInfo), String.valueOf(pm.getApplicationLabel(applicationInfo))); System.out.println(counter); } catch(Exception e){ System.out.println(e.getMessage()); } counter++; } adapter = new AppInfoAdapter(this, R.layout.listview_item_row, app_info); listApplication.setAdapter(adapter); } } 

activity_main.xml带有按钮的ListView

 <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:layout_width="fill_parent" android:id="@+id/listApplication" android:layout_height="fill_parent" android:layout_above="@+id/button1" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="Button" /> </RelativeLayout> 

AppInfoAdapter

 public class AppInfoAdapter extends ArrayAdapter<AppInfo> implements CompoundButton.OnCheckedChangeListener { SparseBooleanArray mCheckStates; Context context; int layoutResourceId; AppInfo data[] = null; public AppInfoAdapter(Context context, int layoutResourceId, AppInfo[] data){ super(context, layoutResourceId,data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; mCheckStates = new SparseBooleanArray(data.length); } @Override public View getView(int position, View convertView, ViewGroup parent){ View row = convertView; AppInfoHolder holder= null; if (row == null){ LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new AppInfoHolder(); holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1); holder.txtTitle = (TextView) row.findViewById(R.id.textView1); holder.chkSelect = (CheckBox) row.findViewById(R.id.checkBox1); row.setTag(holder); } else{ holder = (AppInfoHolder)row.getTag(); } AppInfo appinfo = data[position]; holder.txtTitle.setText(appinfo.applicationName); holder.imgIcon.setImageDrawable(appinfo.icon); // holder.chkSelect.setChecked(true); holder.chkSelect.setTag(position); holder.chkSelect.setChecked(mCheckStates.get(position, false)); holder.chkSelect.setOnCheckedChangeListener(this); return row; } public boolean isChecked(int position) { return mCheckStates.get(position, false); } public void setChecked(int position, boolean isChecked) { mCheckStates.put(position, isChecked); } public void toggle(int position) { setChecked(position, !isChecked(position)); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mCheckStates.put((Integer) buttonView.getTag(), isChecked); } static class AppInfoHolder { ImageView imgIcon; TextView txtTitle; CheckBox chkSelect; } } 

这是快照

在这里输入图像描述

这是一个简化,但很容易…你需要添加焦点标志复选框,如前所述。 您还需要添加可点击标志,如下所示:

 android:focusable="false" android:clickable="false" 

比你控制在ListViewListFragment在我的情况下) onListItemClick事件的复选框状态。

这个示例onListItemClick方法:

 public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); //Get related checkbox and change flag status.. CheckBox cb = (CheckBox)v.findViewById(R.id.rowDone); cb.setChecked(!cb.isChecked()); Toast.makeText(getActivity(), "Click item", Toast.LENGTH_SHORT).show(); } 

您必须添加一个OnItemClickListener到列表视图来确定哪个项目被点击,然后找到复选框。

 mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox_id); } }); 

我有类似的问题。 提供的XML样本放在单个ListViewItem,我不能点击项目本身,但复选框是工作。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/source_container" > <ImageView android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/menu_source_icon" android:background="@drawable/bla" android:layout_margin="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/menu_source_name" android:text="Test" android:textScaleX="1.5" android:textSize="20dp" android:padding="8dp" android:layout_weight="1" android:layout_gravity="center_vertical" android:textColor="@color/source_text_color"/> <CheckBox android:layout_width="40dp" android:layout_height="match_parent" android:id="@+id/menu_source_check_box"/> </LinearLayout> 

解决方案:添加属性

 android:focusable="false" 

复选框控件。

使复选框不可调焦,并在列表项点击做这个,这里codevalue是位置。

  Arraylist<Integer> selectedschools=new Arraylist<Integer>(); lvPickSchool.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int codevalue, long id) { CheckBox cb = (CheckBox) view.findViewById(R.id.cbVisitingStatus); cb.setChecked(!cb.isChecked()); if(cb.isChecked()) { if(!selectedschool.contains(codevaule)) { selectedschool.add(codevaule); } } else { if(selectedschool.contains(codevaule)) { selectedschool.remove(codevaule); } } } }); 

[用CheckBox自定义ListView]

如果customlayout使用复选框,则必须设置复选框focusable = false

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/rowTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:textSize="16sp" > </TextView> <CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:layout_alignParentRight="true" android:layout_marginRight="6sp" android:focusable="false"> // <---important </CheckBox> </RelativeLayout> 

阅读全文: 带有复选框的ListView(不使用ListActivity)

“复选框的使用是确定我选择的Listview中的哪个项目”

  1. 只需使用Adapter类中的setTag()方法将标记添加到复选框即可。 另一边使用getTag()方法。

      @Override public void onBindViewHolder(MyViewHolder holder, int position) { ServiceHelper helper=userServices.get(position); holder.tvServiceName.setText(helper.getServiceName()); if(!helper.isServiceStatus()){ holder.btnAdd.setVisibility(View.VISIBLE); holder.btnAdd.setTag(helper.getServiceName()); holder.checkBoxServiceStatus.setVisibility(View.INVISIBLE); }else{ holder.checkBoxServiceStatus.setVisibility(View.VISIBLE); //This Line holder.checkBoxServiceStatus.setTag(helper.getServiceName()); holder.btnAdd.setVisibility(View.INVISIBLE); } } 
  2. 在复选框的xml代码中,只需将“android:onClick =”您的方法“”属性。

      <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="checkboxClicked" android:id="@+id/checkBox_Service_row" android:layout_marginRight="5dp" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> 
  3. 在你的课上实现这个方法“你的方法”。

      protected void checkboxClicked(View view) { CheckBox checkBox=(CheckBox) view; String tagName=""; if(checkBox.isChecked()){ tagName=checkBox.getTag().toString(); deleteServices.add(tagName); checkboxArrayList.add(checkBox); }else { checkboxArrayList.remove(checkBox); tagName=checkBox.getTag().toString(); if(deleteServices.size()>0&&deleteServices.contains(tagName)){ deleteServices.remove(tagName); } } } 

完整的参考目前在: 列表视图与复选框android studio将选定的项目传递到下一个活动

主要源代码如下。

首先创建一个模型类

 public class Model { private boolean isSelected; private String animal; public String getAnimal() { return animal; } public void setAnimal(String animal) { this.animal = animal; } public boolean getSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } } 

然后在适配器类中,将setTags设置为复选框。 在复选框的onclicklistener中使用这些标签。

 public class CustomAdapter extends BaseAdapter { private Context context; public static ArrayList<Model> modelArrayList; public CustomAdapter(Context context, ArrayList<Model> modelArrayList) { this.context = context; this.modelArrayList = modelArrayList; } @Override public int getViewTypeCount() { return getCount(); } @Override public int getItemViewType(int position) { return position; } @Override public int getCount() { return modelArrayList.size(); } @Override public Object getItem(int position) { return modelArrayList.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.lv_item, null, true); holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb); holder.tvAnimal = (TextView) convertView.findViewById(R.id.animal); convertView.setTag(holder); }else { // the getTag returns the viewHolder object set as a tag to the view holder = (ViewHolder)convertView.getTag(); } holder.checkBox.setText("Checkbox "+position); holder.tvAnimal.setText(modelArrayList.get(position).getAnimal()); holder.checkBox.setChecked(modelArrayList.get(position).getSelected()); holder.checkBox.setTag(R.integer.btnplusview, convertView); holder.checkBox.setTag( position); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View tempview = (View) holder.checkBox.getTag(R.integer.btnplusview); TextView tv = (TextView) tempview.findViewById(R.id.animal); Integer pos = (Integer) holder.checkBox.getTag(); Toast.makeText(context, "Checkbox "+pos+" clicked!", Toast.LENGTH_SHORT).show(); if(modelArrayList.get(pos).getSelected()){ modelArrayList.get(pos).setSelected(false); }else { modelArrayList.get(pos).setSelected(true); } } }); return convertView; } private class ViewHolder { protected CheckBox checkBox; private TextView tvAnimal; } } 

您可以使用模型类并使用setTag() getTag()方法来跟踪列表视图中的哪些项目被选中,哪些不选中。

更多的参考: android中的复选框列表视图

模型的源代码

 public class Model { private boolean isSelected; private String animal; public String getAnimal() { return animal; } public void setAnimal(String animal) { this.animal = animal; } public boolean getSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } } 

把它放在你的自定义适配器中

  holder.checkBox.setTag(R.integer.btnplusview, convertView); holder.checkBox.setTag( position); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View tempview = (View) holder.checkBox.getTag(R.integer.btnplusview); TextView tv = (TextView) tempview.findViewById(R.id.animal); Integer pos = (Integer) holder.checkBox.getTag(); Toast.makeText(context, "Checkbox "+pos+" clicked!", Toast.LENGTH_SHORT).show(); if(modelArrayList.get(pos).getSelected()){ modelArrayList.get(pos).setSelected(false); }else { modelArrayList.get(pos).setSelected(true); } } }); 

整个customAdapter的代码是

 public class CustomAdapter extends BaseAdapter { private Context context; public static ArrayList<Model> modelArrayList; public CustomAdapter(Context context, ArrayList<Model> modelArrayList) { this.context = context; this.modelArrayList = modelArrayList; } @Override public int getViewTypeCount() { return getCount(); } @Override public int getItemViewType(int position) { return position; } @Override public int getCount() { return modelArrayList.size(); } @Override public Object getItem(int position) { return modelArrayList.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.lv_item, null, true); holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb); holder.tvAnimal = (TextView) convertView.findViewById(R.id.animal); convertView.setTag(holder); }else { // the getTag returns the viewHolder object set as a tag to the view holder = (ViewHolder)convertView.getTag(); } holder.checkBox.setText("Checkbox "+position); holder.tvAnimal.setText(modelArrayList.get(position).getAnimal()); holder.checkBox.setChecked(modelArrayList.get(position).getSelected()); holder.checkBox.setTag(R.integer.btnplusview, convertView); holder.checkBox.setTag( position); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View tempview = (View) holder.checkBox.getTag(R.integer.btnplusview); TextView tv = (TextView) tempview.findViewById(R.id.animal); Integer pos = (Integer) holder.checkBox.getTag(); Toast.makeText(context, "Checkbox "+pos+" clicked!", Toast.LENGTH_SHORT).show(); if(modelArrayList.get(pos).getSelected()){ modelArrayList.get(pos).setSelected(false); }else { modelArrayList.get(pos).setSelected(true); } } }); return convertView; } private class ViewHolder { protected CheckBox checkBox; private TextView tvAnimal; } }