I am creating an Android application where I have a ListView that displays all of the applications that were installed in my mobile phone.
My ListView is customized, it is contains a Icon, TextView and CheckBox, the use of the icon is to display the icon of the application, TextView is to display the name of the application, the use of the CheckBox is to determine what item in the ListView that I selected.
How can I determine what is the CheckBox that I selected in the ListView rows when I click a button in my application? I'm new in Android so I don't know what is the approach that I should do.
Here is my code:
public class AppInfo {
public Drawable icon;
public String applicationName;
public AppInfo(){
super();
}
public AppInfo(Drawable icon, String applicationName){
super();
this.icon = icon;
this.applicationName = applicationName;
}
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.widget.CheckBox;
public class AppInfoAdapter extends ArrayAdapter<AppInfo> {
Context context;
int layoutResourceId;
AppInfo data[] = null;
public AppInfoAdapter(Context context, int layoutResourceId, AppInfo[] data){
super(context, layoutResourceId,data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
AppInfoHolder holder= null;
if (row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new AppInfoHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imgPackageIcon);
holder.txtTitle = (TextView) row.findViewById(R.id.txtApplicationName);
holder.chkSelect = (CheckBox) row.findViewById(R.id.chkSelect);
row.setTag(holder);
}
else{
holder = (AppInfoHolder)row.getTag();
}
AppInfo appinfo = data[position];
holder.txtTitle.setText(appinfo.applicationName);
holder.imgIcon.setImageDrawable(appinfo.icon);
holder.chkSelect.setChecked(true);
return row;
}
}
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CheckBox;
public class AppInfoHolder {
ImageView imgIcon;
TextView txtTitle;
CheckBox chkSelect;
}
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.content.pm.PackageInfo;
public class CacheActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cache);
final ListView listApplication = (ListView)findViewById(R.id.listApplication);
ApplicationInfo applicationInfo = getApplicationInfo();
PackageManager pm = getPackageManager();
List<PackageInfo> pInfo = new ArrayList<PackageInfo>();
pInfo.addAll(pm.getInstalledPackages(0));
AppInfo app_info[] = new AppInfo[pInfo.size()];
int counter = 0;
for(PackageInfo item: pInfo){
try{
applicationInfo = pm.getApplicationInfo(item.packageName, 1);
app_info[counter] = new AppInfo(pm.getApplicationIcon(applicationInfo),
String.valueOf(pm.getApplicationLabel(applicationInfo)));
System.out.println(counter);
}
catch(Exception e){
System.out.println(e.getMessage());
}
counter++;
}
AppInfoAdapter adapter = new AppInfoAdapter(this, R.layout.listview_item_row, app_info);
listApplication.setAdapter(adapter);
}
}