so I'm quite new on android development and I'm here to learn. I have been doing some tutorials online for listviews.
I'm on this one now: http://www.survivingwithandroid.com/2013/02/android-listview-adapter-checkbox-item_7.html
Now, I have followed the tutorial, but I have added a little twist of my own. Instead of doing everything in MainActivity
as the tutorial says, I have used a fragment to inflate my views, register for context menu etc. The fragment
is an inner class within my MainActivity.class
.
I'm at the point of the tutorial where I need to add in a CheckBox
in the listview
. This seems fairly easy, so I go and implement the OnCheckedChangeListener
inside my fragment
class.
public class MainActivity extends ActionBarActivity {
//PlaceholderFragment is an inner class of MainActivity
public static class PlaceholderFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {
ListView lv;
public PlaceholderFragment() {
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int pos = lv.getPositionForView(buttonView);
System.out.println("Pos ["+pos+"]");
if (pos != ListView.INVALID_POSITION) {
Planet p = planetsList.get(pos);
p.setChecked(isChecked);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
ListView lv = (ListView) rootView.findViewById(R.id.listView);
aAdpt = new PlanetAdapter(planetsList, getActivity());
lv.setAdapter(aAdpt);
registerForContextMenu(lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
Toast.makeText(getActivity(), "Item with id ["+id+"] - Position ["+position+"] - Genre ["+aAdpt.getItem(position).getDescr()+"]", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
In my PlanetAdapter.class in getView method I have the following code (as per the tutorial):
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
PlanetHolder holder = new PlanetHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.checkbox_layout, parent, false);
TextView tv = (TextView) v.findViewById(R.id.name);
TextView distView = (TextView) v.findViewById(R.id.dist);
CheckBox chk = (CheckBox) v.findViewById(R.id.chk);
holder.chk = chk;
holder.planetNameView = tv;
holder.distView = distView;
**chk.setOnCheckedChangeListener((MainActivity) context);**
v.setTag(holder);
}
else
holder = (PlanetHolder) v.getTag();
Planet p = planetList.get(position);
holder.planetNameView.setText(p.getName());
holder.distView.setText("" + p.getDescr());
holder.chk.setChecked(p.getChecked());
return v;
}
}
Now, when I try to run the code on Eclipse, there is an error because the listener is no longer in the MainActivity chk.setOnCheckedChangeListener((MainActivity) context); - I had move it to the fragment inner class.
Now, I change the code to chk.setOnCheckedChangeListener((MainActivity.PlaceholderFragment) context); and it says I cannot Cast from Context to MainActivity.PlaceholderFragment
.
Does anyone know how I can solve this problem?