listView item listeners
android, checkbox, listview, reference
Solution
Add this
android:focusable="false"
to checkbox in xml.
Assuming you want to dispaly a toast on click of a check box. Try the below
You should use a view holder for smooth scrolling and performance.
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Initialize your listiew in `onCreate`. From your previous code before the edit i could see you intializing listview twice. No need for that.
Change you activity to
public class MainActivity extends Activity {
private List<Random> randomList = new ArrayList<Random>();
ListView list;
ArrayAdapter<Random> adapter;
private SparseBooleanArray mCheckStates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listViewComplex);
adapter = new MyListAdapter();
populateRandomList();
populateListView();
registerClickCallBack();
}
private void populateRandomList() {
// TODO Auto-generated method stub
randomList.add(new Random("Title One", "Simple", "Status: OK", R.drawable.ic_launcher));
randomList.add(new Random("Title Two", "Complex", "Status: WORKING", R.drawable.ic_launcher));
randomList.add(new Random("Title Three", "Moderate", "Status: NOT OK", R.drawable.ic_launcher));
randomList.add(new Random("Title Four", "Simple", "Status: BAD", R.drawable.ic_launcher));
randomList.add(new Random("Title Five", "Moderate", "Status: NOT OK", R.drawable.ic_launcher));
randomList.add(new Random("Title Six", "Simple", "Status: WORKING",R.drawable.ic_launcher));
randomList.add(new Random("Title Seven", "Complex", "Status: EXCELLENT", R.drawable.ic_launcher));
randomList.add(new Random("Title Eight", "Complex", "Status: NBA", R.drawable.ic_launcher));
}
private void populateListView() {
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Random> implements CompoundButton.OnCheckedChangeListener{
public MyListAdapter(){
super(MainActivity.this,R.layout.layout_complex,randomList);
mCheckStates = new SparseBooleanArray(randomList.size());
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return randomList.size();
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder vh;
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.layout_complex,parent,false);
vh = new ViewHolder();
vh.imageView = (ImageView)convertView.findViewById(R.id.itemImage);
vh.titleText = (TextView)convertView.findViewById(R.id.itemTitle);
vh.typeText = (TextView)convertView.findViewById(R.id.itemType);
vh.statusText = (TextView)convertView.findViewById(R.id.itemStatus);
vh.cb = (CheckBox) convertView.findViewById(R.id.checkBoxComplex);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
Random currentItem = randomList.get(position);
vh.imageView.setImageResource(currentItem.getIconID());
vh.titleText.setText(currentItem.getTitle());
vh.typeText.setText(currentItem.getType());
vh.statusText.setText(currentItem.getStatus());
vh.cb.setTag(position);
vh.cb.setChecked(mCheckStates.get(position, false));
vh.cb.setOnCheckedChangeListener(this);
return convertView;
}
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);
if(isChecked)
{
Toast.makeText(MainActivity.this, "Checked at "+(Integer) buttonView.getTag(), Toast.LENGTH_SHORT).show();
}
}
class ViewHolder
{
TextView titleText,typeText,statusText;
ImageView imageView;
CheckBox cb;
}
}
private void registerClickCallBack() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
long id) {
Random clickedRandom = randomList.get(position);
String message = "You have clicked position "+position+" which is titled "+clickedRandom.getTitle();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
}
Snap shot
Problem
When using a `listView` I noticed that, on setting up a `checkBox` in a listView, the `listView.OnItemClicked` listener does not work ! For now, I am trying to implement two different `on click listeners` for the `listView`. The first one is for the listView body and the second one for the listView's body's checkBox. The listView's body is to (for now) generate a toast and the checkBox is to generate a another toast(for now). The problem I am facing is when referring to `checkBox.onClick` the logic is only implemented to the last(or any random,but only one) checkbox in the layout. The listView layout looks like this: MainActivity.java: ``` private void registerClickCallBack() { // TODO Auto-generated method stub ListView list = (ListView)findViewById(R.id.listViewComplex); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) { // TODO Auto-generated method stub Random clickedRandom = randomList.get(position); String message = "You have clicked position "+position+" which is titled "+clickedRandom.getTitle(); Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show(); final CheckBox check= (CheckBox)findViewById(R.id.checkBoxComplex); check.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(check.isChecked()){ Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_LONG).show(); } } }); } }); } } ```