I need to create events for clicking the checkbox, but it only takes effect for the first screen. My code :
Would be very happy if someone help me up.
public class ListaFragment extends ListFragment{
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//ListView lv = getListView(); // here it is available
final View rootView = inflater.inflate(R.layout.fragment_lista, container, false);
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongsManager plm = new SongsManager();
// get all songs from sdcard
Button exe = (Button) rootView.findViewById(R.id.Execultar);
exe.setOnClickListener(new OnClickListener() {
public ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
@Override
public void onClick(View v) {
SongsManager novo = new SongsManager();
this.list = novo.getPlayList();
CheckBox android = (CheckBox) rootView.findViewById(R.id.checkBox1);
android.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.isClickable()){
Toast.makeText(getActivity(), "Cliquei", Toast.LENGTH_LONG).show();
}
}
});
//ArrayList<HashMap<String, String>> lista = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < list.size(); i++) {
//CheckBox android = (CheckBox) rootView.findViewById(R.id.checkBox1);
//CheckBox ck = (CheckBox) v;
Toast.makeText(getActivity(), "Cliquei "+i+'-'+android.isChecked()+'-'+list.size(), Toast.LENGTH_LONG).show();
}
}
});
this.songsList = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
ListAdapter adapter = new SimpleAdapter(this.getActivity(), songsListData,
R.layout.fragment_item, new String[] { "songTitle" }, new int[] {
R.id.songTitle });
setListAdapter(adapter);
return rootView;
}
}
Is everything right with the code? What are the changes that I have to make in the code to achieve the task that I am looking for.
In onCreateView
you are setting a click listener on the first checkbox in the list. At this point in time there is no way to know how many items will be in your list or iterate through them, so you are going to want to remove that part of your code. I can suggest two possible solutions.
The first is much simpler, but with one major drawback. You will not be able to tell if they clicked on the check box, or the song title. If that is not a problem for you, this is the easiest solution. Attach an OnItemClickListener
to your listview. It will tell you which row in the list was clicked.
// get our list
ListView listView = rootView.findViewById(R.id.list);
// set the click listener on the whole list
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (view.isClickable()){
Toast.makeText(getActivity(), "Cliquei row # " + position, Toast.LENGTH_LONG).show();
}
}
});
If you need to know that they are clicking on the checkbox specifically, then you will have to do a little extra work. Instead of using a SimpleAdapter
you are going to want to make your own adapter class that extends SimpleAdapter
. Inside your new adapter class you will override getView(int position, View convertView, ViewGroup parent)
. This method will be used to create each view in your list. As the list view is created, you will attach an OnCheckedChangeListener
to the checkbox on that particular view. Something like the following...
public class MyListAdapter extends SimpleListAdapter() {
@Override
public view getView(int position, View convertView, ViewGroup parent) {
// let the superclass inflate our view for us
View view = super.getView(position, convertView, parent);
// attach a listener to our check box
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(getActivity(), "row # " + position + " is checked", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "row # " + position + " is unchecked", Toast.LENGTH_LONG).show();
}
}
});
}
}