Change this
chk.setOnCheckedChangeListener((MainActivity) context)
to
chk.setOnCheckedChangeListener(PlaceholderFragment.this)
Your Fragment implements the interface CompoundButton.OnCheckedChangeListener
not your Activity
public static class PlaceholderFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {
Edit:
Seems you have checkbox in Adapter class
So you need your PlanetAdapter
to implement CompoundButton.OnCheckedChangeListener
Change to
chk.setOnCheckedChangeListener(PlanetAdapter.this);
And there is no need for Fragment to implement the interface. So change to
PlaceholderFragment extends Fragment
Example:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.replace(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
ApplicationInfo applicationInfo = getActivity().getApplicationInfo();
PackageManager pm = getActivity().getPackageManager();
List<PackageInfo> pInfo = new ArrayList<PackageInfo>();
pInfo.addAll(pm.getInstalledPackages(0));
final 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){
e.printStackTrace();
}
counter++;
}
ListView listApplication = (ListView)rootView.findViewById(R.id.listView1);
final AppInfoAdapter adapter = new AppInfoAdapter(getActivity(), app_info);
listApplication.setAdapter(adapter);
Button b= (Button)rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuilder result = new StringBuilder();
for(int i=0;i<app_info.length;i++)
{
if(adapter.mCheckStates.get(i)==true)
{
result.append(app_info[i].applicationName);
result.append("\n");
}
}
Toast.makeText(getActivity(), result, 1000).show();
}
});
return rootView;
}
}
}
AppInfo.java
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;
}
}
AppInfoAdapter.java
public class AppInfoAdapter extends ArrayAdapter<AppInfo> implements CompoundButton.OnCheckedChangeListener
{ SparseBooleanArray mCheckStates;
Context context;
AppInfo data[] = null;
LayoutInflater mInflater;
public AppInfoAdapter(Context context, AppInfo[] data){
super(context, 0,data);
this.context = context;
this.data = data;
mInflater = LayoutInflater.from(context);
mCheckStates = new SparseBooleanArray(data.length);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
AppInfoHolder holder= null;
if (row == null){
row = mInflater.inflate(R.layout.list_item, parent, false);
holder = new AppInfoHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
holder.txtTitle = (TextView) row.findViewById(R.id.textView1);
holder.chkSelect = (CheckBox) row.findViewById(R.id.checkBox1);
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);
holder.chkSelect.setTag(position);
holder.chkSelect.setChecked(mCheckStates.get(position, false));
holder.chkSelect.setOnCheckedChangeListener(this);
return row;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
static class AppInfoHolder
{
ImageView imgIcon;
TextView txtTitle;
CheckBox chkSelect;
}
}
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.listviewcheckbox.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_above="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp" >
</ListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="21dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="32dp"
android:src="@drawable/ic_launcher" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginRight="14dp"
android:text="CheckBox" />
</RelativeLayout>
activity_main.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.example.listviewcheckbox.MainActivity"
tools:ignore="MergeRootFrame" />
Snap