2012-08-07 188 views
0

我創建了一個具有圖像+文本+複選框的自定義列表視圖。如何檢測複選框的選擇/取消選擇。我嘗試了onClick(),但它不會被調用。我是否正確使用它?Listview複選框選擇

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; 


} 

} 

我應該在哪裏添加此代碼(如果這是正確的聽者),

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); 
     } 
    }); 

謝謝..

+0

爲什麼你在'ListView'上設置'onClickListener()'而不是在'checkBox'上設置它 – Nishant 2012-08-07 06:45:16

回答

1

寫內部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; 


} 
+0

感謝這正是我一直在尋找的:) – user340 2012-08-07 10:27:27

+0

我想使用後退按鈕按下列表視圖。但是我不能在InstalledAppsAdapter中定義它。所以我必須在InstalledApps類中定義它。當我按下列表視圖中的後退按鈕時,它會被調用,但在主視圖中不會被調用。我想要在背面按下時捕獲這兩個事件。任何想法如何做到這一點? – user340 2012-08-08 01:22:05

+0

在新主題中提出您的問題 – Nishant 2012-08-08 13:07:05

1

的insted的代碼保持複選框可以在選擇模式下使用simple_list_item_multiple_choice。