Reputation: 4408
I have a listview with each row having a checkbox, imageview and text view
my requirements are 1. checkbox can be checkd and unchecked and for each click on checkbox for each row do function1 : This i could do by defining an onitem clicklistenr for the check box within the getview of the baseadapter
So my question is is that the right way to do it, define onitemclick listener for all other items and make it call function 2 ?
Or is there a way i can achieve the row selection for my list. Please see my code below . I can give any further details if needed
list.java
public class ScanListActivity extends BaseActivity {
static Button scanlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.scan_list);
List<ScannedProduct> productList = new ArrayList<ScannedProduct>();
productList = getProductList();
final ListView lv = (ListView) findViewById(R.id.list);
scanlist = (Button) findViewById(R.id.addtowishlist);
scanlist.setEnabled(false);
lv.setAdapter(new ProductListAdapter(this, productList, scanlist));
}
adapter ;getview
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final int pos = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.product_list_row, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.productid);
holder.text1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i("List",""+pos);
}
});
holder.text2 = (TextView) convertView.findViewById(R.id.price);
holder.image = (ImageView) convertView
.findViewById(R.id.productimageid);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(productList.get(position).getTitle());
holder.text2.setText(productList.get(position).getPrice().toString());
if(productList.get(position).getSmallImage() != null){
byte [] bb= (productList.get(position).getSmallImage());
holder.image.setImageBitmap(BitmapFactory.decodeByteArray(bb, 0, bb.length));
}
else{
holder.image.setImageBitmap(null);
holder.image.setBackgroundResource(R.drawable.highlight_disabled);
}
// holder.image.setImageBitmap(Utils.loadBitmap(productList.get(position).getSmallImage()));
final CheckBox check = (CheckBox) convertView
.findViewById(R.id.checkbox);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (check.isChecked()) {
ProductListAdapter.count++;
} else {
ProductListAdapter.count--;
}
if (ProductListAdapter.count == 0) {
//showOrHideButton.setClickable(false);
//showOrHideButton.setVisibility(View.GONE);
showOrHideButton.setEnabled(false);
} else {
//showOrHideButton.setVisibility(View.VISIBLE);
showOrHideButton.setEnabled(true);
}
}
});
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView image;
TextView text2;
}
Upvotes: 0
Views: 730
Reputation: 38168
There are 2 options :
as you do : define a OnClickListener for each view. But then note that all items could share the same listener (thus declared as a field and not inlined as you did), this would use less memory.
use a OnItemClickListener
The second one is more practical but I remember there is a limitation using it but can't remember what it was. I personnaly use the first strategy.
Upvotes: 1