但我的问题没有得到解决。我知道关于这个问题有很多问题,但我的问题是
我有 4 个 A、B、C、D 类
从 A 类我转到 B 类,列表视图中列出了所有已安装的应用程序。在 B 类中,我将 Packagelist 发送到 C 类(BaseAdapter 类),然后用户选择的应用程序将传递给 A 类。
所以我的问题是我在哪里或如何使用 POJO 类将已安装的应用程序传递给 baseadapter 类,然后我如何获取所有检查值(字符串应用程序名称)并将其传递给 A 类。以及我如何保持检查状态复选框。
任何帮助请。
自定义适配器类
import java.util.List;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
public class AppListAdapter extends BaseAdapter {
List<PackageInfo> packageList;
Activity context;
PackageManager packageManager;
boolean[] itemChecked;
int checkBoxCounter = 0;
int checkBoxInitialized = 0;
public AppListAdapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
itemChecked = new boolean[packageList.size()];
}
private class ViewHolder {
TextView apkName;
CheckBox ck1;
}
public int getCount() {
return packageList.size();
}
public Object getItem(int position) {
return packageList.get(position);
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// final ViewHolder holder;
checkBoxCounter = 0;
checkBoxInitialized = 0;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.apklist_item, null);
final ViewHolder holder = new ViewHolder();
holder.apkName = (TextView) convertView
.findViewById(R.id.ApkList_tvappname);
holder.ck1 = (CheckBox) convertView
.findViewById(R.id.ApkList_checkBox);
holder.ck1
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
AppsSelected element = (AppsSelected) holder.ck1
.getTag();
element.setSelected(buttonView.isChecked());
if (checkBoxCounter <= checkBoxInitialized) {
// increment counter, when we scroll the List it
// execute onCheckedChanged everytime so by
// using this stuff we can maintain the state
checkBoxCounter++;
} else {
element = (AppsSelected) holder.ck1.getTag();
element.setSelected(buttonView.isChecked());
if (element.isSelected())
Toast.makeText(
context,
"You selected " + element.getName(),
Toast.LENGTH_LONG).show();
else
Toast.makeText(
context,
"Not selected " + element.getName(),
Toast.LENGTH_LONG).show();
}
}
});
convertView.setTag(holder);
holder.ck1.setTag(packageList.get(position));
} else {
((ViewHolder) convertView.getTag()).ck1.setTag(packageList.get(position));
}
ViewHolder holder = (ViewHolder) convertView.getTag();
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
appIcon.setBounds(0, 0, 40, 40);
holder.apkName.setCompoundDrawables(appIcon, null, null, null);
holder.apkName.setCompoundDrawablePadding(15);
holder.apkName.setText(appName);
return convertView;
}
}