我的ListView不正常 [英] My ListView not working properly

查看:219
本文介绍了我的ListView不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的一个问题,而在布局的ListView增加。我曾在一个页面实现的一个ListView中我们得到的产品清单,因为当我们点击一​​些ListMember它改变其颜色,并再次点击将其更改为previous color.Now想象,因为项目的高度一屏可容纳最多5个列表项,为下一个成员,看你需要向下滚动。

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.

现在想象列表成员

项目1

项目2

项目3

第4项

第5项

第6项

第7项

在这些应用只能看到5个项目的时间,现在当我点击项目1(前五个成员的第一个成员),它的颜色变化(比如白到绿),但是当我向下滚动我看到'项目6'(第一五名成员的第一个成员)也改变了它的颜色(绿色),当我点击项目6',此时 setOnItemClickListener 该成员越来越实际触发并试图改变它的颜色是什么已经改变了。

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.

这是code为 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 :: 这是我的适配器

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;

}

}

为什么发生这种情况?

推荐答案

做你想要什么,你要一个适配器添加到您的ListView和有控制对每个项目的点击方式。

to do what you want, you have to add an adapter to your listview and there control the on click method for each item.

与实例更新

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

这篇关于我的ListView不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆