Problem description:
I created a custom listview with Image, Text, and checkbox. Check whether or not to select the check box. I used onItemClick () but it was not called. Are I correct?
[Java]
Class PInfo {
String appname = "";
String pname = "";
Drawable icon;
}
Public class InstalledApps extends Activity {
Private static LayoutInflater inflater = null;
Private ListView listview;
Private 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 );
}
}
Installedtransferadapter iap = new installedtransferadapter (this, installedApps );
Listview. setAdapter (iap );
}
}
Class installed+adapter extends BaseAdapter {
Private Activity;
Private static LayoutInflater inflater = null;
List <PInfo> installedApps = new ArrayList <PInfo> ();
ListView listview;
Public installed+adapter (Activity a, List <PInfo> B ){
// TODO Auto-generated constructor stub
InstalledApps = B;
Activity =;
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;
}
}
Class PInfo {
String appname = "";
String pname = "";
Drawable icon;
}
Public class InstalledApps extends Activity {
Private static LayoutInflater inflater = null;
Private ListView listview;
Private 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 );
}
}
Installedtransferadapter iap = new installedtransferadapter (this, installedApps );
Listview. setAdapter (iap );
}
}
Class installed+adapter extends BaseAdapter {
Private Activity;
Private static LayoutInflater inflater = null;
List <PInfo> installedApps = new ArrayList <PInfo> ();
ListView listview;
Public installed+adapter (Activity a, List <PInfo> B ){
// TODO Auto-generated constructor stub
InstalledApps = B;
Activity =;
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;
}
} Where can I add the following code (if this is the correct listner)
[Java]
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 );
}
});
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 );
}
});
Solution:
Add the following code to getView:
[Java]
@ 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;
}
@ 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;
}