简体   繁体   English

我的ListView无法正常工作

[英]My ListView not working properly

I am getting one problem while adding ListView in layout. 我在布局中添加ListView时遇到一个问题。 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. 我在一个页面中实现了一个ListView,我们得到了项目列表,当我们点击某个ListMember时,它会改变它的颜色并再次单击它会将它改变为以前的颜色。现在想象一下,因为项目高度,一个屏幕可以保持最大值5列出项目,为下一个成员查看您需要向下滚动。

Now imagine List members are 现在想象List成员了

Item 1 第1项

Item 2 第2项

Item 3 第3项

Item 4 第4项

Item 5 第5项

Item 6 项目6

Item 7 第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. 在这些使用中,一次只能看到5个项目,现在当我点击“项目1”(前五个成员的第一个成员)时,它的颜色正在改变(比如白色到绿色)但是当我向下滚动时我看到'项目6' (前五个成员的第一个成员)也改变了它的颜色(到绿色),当我点击'Item 6'时,这个成员的setOnItemClickListener实际上被触发并尝试将其颜色改为已经改变的颜色。

this is code for setOnItemClickListener : 这是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 AfterEdit ::这是我的适配器

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 ? 为什么会这样?

to do what you want, you have to add an adapter to your listview and there control the on click method for each item. 要做你想做的事,你必须在listview中添加一个适配器,然后控制每个项目的on click方法。

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;
    }
}

Do as following 做如下

1 Handle click event in your adapter not Activity 1处理适配器中的单击事件而不是“活动”

2 View for your click can be parent layout in item_list.xml 2单击视图可以是item_list.xml中的父布局

3 Don't use final int[] listClicked instead have boolean variable in Product class for example isChecked. 3不要使用final int [] listClicked而是在Product类中使用boolean变量,例如isChecked。

4 Set and unset isChecked on click and set background color 4设置和取消设置isChecked单击并设置背景颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM