Listview复选框的选择

问答小助手 2013-04-23 02:19:14
加精
原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/2175

问题描述:

我创建了一个自定义的 listview,有 Image,Text 和 checkbox。如何检测是否选择或未选择复选框。我使用了 onItemClick() 但是没有被调用。我使用的正确吗?
class PInfo {
String appname = "";
String pname = "";
Drawable icon;
}
public class InstalledApps extends Activity{
private static LayoutInflater inflater=null;
private ListView listview;
private Activity activity;
List<PInfo> installedApps = new ArrayList<PInfo>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.applist);
activity = this;
listview = (ListView)findViewById(R.id.listView1);
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo app : packages) {
PInfo newInfo = new PInfo();
newInfo.appname = (String) app.loadLabel(pm);
newInfo.pname = app.packageName;
//newInfo.icon = app.icon;
//Drawable dd = activity.getResources().getDrawable(app.icon);
newInfo.icon = app.loadIcon(getPackageManager());
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
installedApps.add(newInfo);
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
installedApps.add(newInfo);
} else {
installedApps.add(newInfo);
}
}
InstalledAppsAdapter iap = new InstalledAppsAdapter(this, installedApps);
listview.setAdapter(iap);
}
}
class InstalledAppsAdapter extends BaseAdapter{
private Activity activity;
private static LayoutInflater inflater=null;
List<PInfo> installedApps = new ArrayList<PInfo>();
ListView listview;
public InstalledAppsAdapter(Activity a, List<PInfo> b) {
// TODO Auto-generated constructor stub
installedApps = b;
activity = a;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listview = (ListView)activity.findViewById(R.id.listView1);
//Here is where I have put it currently..
listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
PInfo newInfo = (PInfo) listview.getItemAtPosition(arg2);
System.out.println(newInfo.appname);
}
});
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return installedApps.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View customView = inflater.inflate(R.layout.listrow, arg2,false);
ImageView iv = (ImageView)customView.findViewById(R.id.imageView1);
TextView tv= (TextView) customView.findViewById(R.id.textView2);
CheckBox cb= (CheckBox)customView.findViewById(R.id.checkBox1);
PInfo pinfo = installedApps.get(arg0);
tv.setText(""+pinfo.appname);
iv.setImageDrawable(pinfo.icon);
return customView;
}
}


我要在哪里添加下面的代码(如果这是正确的listner)

listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
PInfo newInfo = (PInfo) listview.getItemAtPosition(arg2);
System.out.println(newInfo.appname);
}
});


解决方案:

在 getView 里面加入下面的代码:

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View customView = inflater.inflate(R.layout.listrow, arg2,false);
ImageView iv = (ImageView)customView.findViewById(R.id.imageView1);
TextView tv= (TextView) customView.findViewById(R.id.textView2);
CheckBox cb= (CheckBox)customView.findViewById(R.id.checkBox1);

cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
}
PInfo pinfo = installedApps.get(arg0);

tv.setText(""+pinfo.appname);
iv.setImageDrawable(pinfo.icon);
return customView;
}
...全文
1185 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
scyyzgxh 2013-04-28
  • 打赏
  • 举报
回复
帮顶一下。。。
aHaile 2013-04-27
  • 打赏
  • 举报
回复
厉害厉害!!
leichentao094 2013-04-26
  • 打赏
  • 举报
回复
学习了,有时间研究一下
zhangzhang1226 2013-04-26
  • 打赏
  • 举报
回复
帮顶了
telso111 2013-04-25
  • 打赏
  • 举报
回复
不错 很多人有方面的困扰
haoyizsw 2013-04-25
  • 打赏
  • 举报
回复
不错 很多人有方面的困扰
u010135157 2013-04-25
  • 打赏
  • 举报
回复
在学习中。
zyr987503101 2013-04-25
  • 打赏
  • 举报
回复
JAVA 看不懂
2013-04-24
  • 打赏
  • 举报
回复
15648453165
jimette 2013-04-24
  • 打赏
  • 举报
回复
vwenyu-L 2013-04-24
  • 打赏
  • 举报
回复
学习了
欢喜就好 2013-04-24
  • 打赏
  • 举报
回复
ma2948 2013-04-24
  • 打赏
  • 举报
回复
不错,学习了
u010436780 2013-04-24
  • 打赏
  • 举报
回复
u010436780 2013-04-24
  • 打赏
  • 举报
回复
detective2005 2013-04-24
  • 打赏
  • 举报
回复
subaohao 2013-04-23
  • 打赏
  • 举报
回复
学而不思则罔,思而不学则殆
u010426912 2013-04-23
  • 打赏
  • 举报
回复
学习了,很好啊
gyfywt 2013-04-23
  • 打赏
  • 举报
回复
这个好复杂啊
mbugaifc 2013-04-23
  • 打赏
  • 举报
回复
加载更多回复(11)

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧