Reputation: 868
I am getting one problem while adding ListView in layout. I have implemented one ListView in one page where we get list of items, in that when we click on some ListMember it change its color and again clicking on it will change it to previous color.Now imagine because of Item height one screen can hold maximum 5 List items,for next member to see you need to scroll down.
Now imagine List members are
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Among these use can only see 5 items at a time, now when I click on 'Item 1'(first member of first five members) its color is changing(say WHITE TO GREEN) but when I scroll down I see 'Item 6'(first member of first five members) is also changed its color(to GREEN),and when I click on 'Item 6' ,this time setOnItemClickListener
for that member is getting actually triggered and trying changing its color to what it already changed.
this is code for setOnItemClickListener
:
productList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Log.i("imIn","Item Clicked");
v.animate();
if(listClicked[position]==0)
{
Log.i("***After*** ","Cyan Set ON");
v.setBackgroundColor(Color.parseColor("GREEN"));
listClicked[position]=1;
}
else if(listClicked[position]==1){
Log.i("***After*** ","Cyan Set OFF");
v.setBackgroundColor(Color.parseColor("WHITE"));
listClicked[position]=0;
}
}
});
AfterEdit:: this is my adapter
public class ProductListBaseAdapter extends BaseAdapter {
SharedPreferences sharedpreferences;
private static ArrayList<Product> searchArrayList;
private LayoutInflater mInflater;
ArrayList<TotalSelectedProduct> selectedProducts=new ArrayList<>();
final int[] listClicked;
public ProductListBaseAdapter(Context context, ArrayList<Product> totalProducts, int[] ClickedList) {
searchArrayList =totalProducts;
mInflater = LayoutInflater.from(context);
listClicked=ClickedList;
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_list, null);
holder = new ViewHolder();
holder.txtItem = (TextView) convertView.findViewById(R.id.item_name);
holder.edit=(Button)convertView.findViewById(R.id.edit);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
/** I have tried inserting onClickListener in adapter also .but resulting same
*
holder.txtItem.setText(searchArrayList.get(position).getItemName());
final View.OnClickListener makeListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.animate();
if(listClicked[position]==0)
{
Log.i("***After*** ","Cyan Set ON");
v.setBackgroundColor(Color.parseColor("#ff2dbeff"));
listClicked[position]=1;
}
else if(listClicked[position]==1){
Log.i("***After*** ","Cyan Set OFF");
v.setBackgroundColor(Color.parseColor("#009933"));
listClicked[position]=0;
}
}
};
holder.txtItem.setOnClickListener(makeListener); */
return convertView;
}
static class ViewHolder {
TextView txtItem;
Button edit;
}
}
Why this is happening ?
Upvotes: 1
Views: 432
Reputation: 3973
to do what you want, you have to add an adapter to your listview and there control the on click method for each item.
UPDATE WITH EXAMPLE
public class ProductAdapter extends ArrayAdapter<Product> {
public PProductAdapter(Activity activity,
ArrayList<Product> products) {
super(activity, R.layout.item_product, products);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Product p = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_product, parent,
false);
viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
p.checked = !p.checked;
if (p.checked)
v.setBackgroundColor(Color.parseColor("#ff2dbeff"));
else
v.setBackgroundColor(Color.parseColor("#009933"));
}
});
viewHolder.name.setText(p.name);
return convertView;
}
private class ViewHolder {
TextView name;
}
}
public class Product{
public String name;
public boolean checked;
Product() {
name = "dummy name";
checked = false;
}
}
Upvotes: 2
Reputation:
Do as following
1 Handle click event in your adapter not Activity
2 View for your click can be parent layout in item_list.xml
3 Don't use final int[] listClicked instead have boolean variable in Product class for example isChecked.
4 Set and unset isChecked on click and set background color
Upvotes: 1