SLaks 2013-04-22 05:01 采纳率: 0%
浏览 5199
已采纳

Listview复选框的选择

我创建了一个自定义的 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);
        }
    });

谢谢!

  • 写回答

2条回答 默认 最新

  • xiaoyan_12 2013-04-22 08:29
    关注

    在 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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀