Java 带有复选框onclicklistener的android listview不工作
我有一个带有自定义适配器的listview。在listview上我有textview图像和复选框。工作正常。当我点击项目时,我有一个arraylist,该项目值存储在arraylist中,当我再次点击项目值从数组列表中移除时,效果很好。问题是当我点击复选框值不存储在arraylist中时,问题是什么我尽了最大努力但没有解决我的问题。我想当我点击项目或复选框时,它的值存储在arraylist中,这里是我的自定义适配器代码Java 带有复选框onclicklistener的android listview不工作,java,android,listview,arraylist,checkbox,Java,Android,Listview,Arraylist,Checkbox,我有一个带有自定义适配器的listview。在listview上我有textview图像和复选框。工作正常。当我点击项目时,我有一个arraylist,该项目值存储在arraylist中,当我再次点击项目值从数组列表中移除时,效果很好。问题是当我点击复选框值不存储在arraylist中时,问题是什么我尽了最大努力但没有解决我的问题。我想当我点击项目或复选框时,它的值存储在arraylist中,这里是我的自定义适配器代码 public class Listadapter extends BaseA
public class Listadapter extends BaseAdapter {
CheckBox checkBox;
boolean index[];
boolean[] itemChecked;
ResolveInfo entry;
String[] itempkg;
private Context mContext;
private List<ResolveInfo> mListAppInfo;
private PackageManager mPackManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();
public Listadapter(Context applicationContext, List<ResolveInfo> installedApplication, PackageManager packageManager)
{
//super(applicationContext,textViewResourceId,installedApplication);
super();
this.mContext = applicationContext;
this.mListAppInfo = installedApplication;
index = new boolean[installedApplication.size()];
this.mPackManager = packageManager;
for (int i = 0; i < installedApplication.size(); i++) {
checkList.add(false);
itemChecked = new boolean[installedApplication.size()];
itempkg = new String[installedApplication.size()];
}
}
@Override
public int getCount()
{
return mListAppInfo.size();
//return ((null != mListAppInfo) ? mListAppInfo.size() : 0);
}
@Override
public Object getItem(int position)
{
// index = new boolean[mListAppInfo.size()];
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// get the selected entry
final ViewHolder holder;
// LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
// reference to convertView
holder.tvAppName = (TextView) convertView
.findViewById(R.id.textView1);
holder.tvPkgName = (TextView) convertView
.findViewById(R.id.textView);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.ivAppIcon = (ImageView) convertView
.findViewById(R.id.imageView);
convertView.setTag(holder);
// holder.ck1.setTag(packageList.get(position));
} else {
holder = (ViewHolder) convertView.getTag();
}
entry = mListAppInfo.get(position);
holder.ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
holder.tvAppName.setText(entry.loadLabel(mPackManager));
holder.tvPkgName.setText(entry.activityInfo.packageName);
holder.checkBox.setChecked(false);
if (itemChecked[position])
holder.checkBox.setChecked(true);
else
holder.checkBox.setChecked(false);
holder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.checkBox.isChecked()) {
itemChecked[position] = true;
} else {
itemChecked[position] = false;
}
}
});
return convertView;
}
private class ViewHolder
{
ImageView ivAppIcon;
TextView tvAppName;
TextView tvPkgName;
CheckBox checkBox;
}
}
公共类Listadapter扩展了BaseAdapter{
复选框;
布尔索引[];
布尔[]项检查;
解析信息录入;
字符串[]itempkg;
私有上下文;
私人名单;
专用软件包管理器mPackManager;
私有ArrayList检查表=新建ArrayList();
公共Listadapter(上下文应用程序上下文、列表安装的应用程序、PackageManager PackageManager)
{
//超级(applicationContext、textViewResourceId、installedApplication);
超级();
this.mContext=applicationContext;
this.mListAppInfo=已安装的应用程序;
索引=新布尔值[installedApplication.size()];
this.mPackManager=packageManager;
对于(int i=0;i
这是我的活动代码
public class Profile5Activity extends Activity implements AdapterView.OnItemClickListener {
ListView apps5;
PackageManager packageManager5;
static ArrayList<String> checkedValue5;
Button bt5;
ResolveInfo pi5 = new ResolveInfo();
Context context = this;
CheckBox cb5;
Listadapter Adapter5 = null;
boolean[] itemChecked5;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile5);
itemChecked5 = new boolean[AllApps.getInstalledApplication(this).size()];
checkedValue5 = new ArrayList<String>();
}
apps5 = (ListView) findViewById(R.id.list5);
apps5.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
packageManager5 = getPackageManager();
List<String> list = new ArrayList<String>();
Adapter5 = new Listadapter(this, AllApps.getInstalledApplication(this), packageManager5);
apps5.setAdapter(Adapter5);
apps5.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, final int position, long arg3)
{
// TODO Auto-generated method stub
cb5 = (CheckBox) v.findViewById(R.id.checkBox1);
final TextView tv5 = (TextView) v.findViewById(R.id.textView);
final TextView tvv5 = (TextView) v.findViewById(R.id.textView1);
pi5 = (ResolveInfo) arg0.getItemAtPosition(position);
cb5.performClick();
if (cb5.isChecked()) {
checkedValue5.add(tv5.getText().toString());
itemChecked5[position] = true;
Toast.makeText(context, "all :" +checkedValue5, Toast.LENGTH_SHORT).show();
} else if (!cb5.isChecked()) {
checkedValue5.remove(tv5.getText().toString());
Toast.makeText(context, "all :" +checkedValue5, Toast.LENGTH_SHORT).show();
}
}
公共类Profile5Activity扩展活动实现AdapterView.OnItemClickListener{
列表视图apps5;
PackageManager PackageManager 5;
静态ArrayList checkedValue5;
按钮bt5;
ResolveInfo pi5=新的ResolveInfo();
上下文=这个;
复选框cb5;
ListAdapter5=null;
布尔[]项检查5;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile5);
itemChecked5=新布尔值[AllApps.getInstalledApplication(this.size()];
checkedValue5=新的ArrayList();
}
apps5=(列表视图)findViewById(R.id.list5);
app5.setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
PackageManager 5=getPackageManager();
列表=新的ArrayList();
Adapter5=新的Listadapter(this,AllApps.getInstalledApplication(this),PackageManager 5);
附件5.设置适配器(适配器5);
附录5.SetonicClickListener(本);
}
@凌驾
公共点单击(AdapterView arg0,视图v,最终整型位置,长arg3)
{
//TODO自动生成的方法存根
cb5=(复选框)v.findviewbyd(R.id.checkBox1);
最终文本视图tv5=(文本视图)v.findViewById(R.id.TextView);
最终文本视图TV5=(文本视图)v.findViewById(R.id.textView1);
pi5=(ResolveInfo)arg0.getItemAtPosition(position);
cb5.performClick();
如果(cb5.isChecked()){
checkedValue5.add(tv5.getText().toString());
itemChecked5[位置]=真;
Toast.makeText(上下文,“all:+checkedValue5,Toast.LENGTH_SHORT).show();
}如果(!cb5.isChecked()){
checkedValue5.remove(tv5.getText().toString());
Toast.makeText(con
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="55dp"
android:layout_height="55dp"
android:button="@drawable/checkbox_selector"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/textView"
android:textSize="11dp"
android:layout_width="266dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignStart="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:paddingLeft="9dp"
android:id="@+id/imageView"
android:layout_width="55dp"
android:layout_height="55dp" />
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list5"
android:dividerHeight="0dp"
android:listSelector="#00000000"
android:smoothScrollbar="true"
android:scrollingCache="false"
android:animationCache="false"
android:layout_centerHorizontal="true"
android:layout_below="@+id/ed44" />