Activity
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.widget.ListView;
public class AppScreen extends Activity {
private ListView list;
ArrayList<Datamodel> res;
MyAdapter _adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_screen);
list = (ListView) findViewById(R.id.list);
List<PackageInfo> _myapps = getPackageManager().getInstalledPackages(0);
res = new ArrayList<Datamodel>();
for (int i = 0; i < _myapps.size(); i++) {
PackageInfo p = _myapps.get(i);
Datamodel _model = new Datamodel();
_model.setAppname(p.applicationInfo.loadLabel(getPackageManager()).toString());
res.add(_model);
System.out.println("ajajajja" + res.size() + res.get(i).getAppname());
}
_adapter = new MyAdapter(getApplicationContext(), res);
_adapter.notifyDataSetChanged();
list.setAdapter(_adapter);
}
}
Datamodel class
public class Datamodel {
public String appname = "";
private boolean selected;
public String getAppname() {
return appname;
}
public void setAppname(String appname) {
this.appname = appname;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean isChecked) {
this.selected = isChecked;
}
}
Adapter class
public class MyAdapter extends BaseAdapter {
Context _ctx;
LayoutInflater inflater;
public ArrayList<Datamodel> data;
public MyAdapter(Context c, ArrayList<Datamodel> _arraylist) {
this._ctx = c;
this.data = _arraylist;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) _ctx.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.inflator, parent, false);
final CheckBox checks = (CheckBox) itemView.findViewById(R.id.b);
final TextView _setappname = (TextView) itemView.findViewById(R.id.a);
checks.setChecked(data.get(position).isSelected());
Datamodel obj = data.get(position);
_setappname.setText(obj.getAppname());
checks.setTag(position);
checks.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
Datamodel _obj = data.get(getPosition);
data.get(position).setSelected(isChecked);
String ss = _obj.getAppname();
System.out.println("pos is" + getPosition);
}
});
return itemView;
}
}
Activity XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.installedapps.AppScreen" >
<ListView
android:id="@+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
Inflator xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="@+id/a"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:textColor="#000"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1.7" />
<CheckBox
android:id="@+id/b"
android:layout_width="0dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="0.3"
android:layout_height="wrap_content" />
</LinearLayout>