在运行时更改自定义列表视图的值

时间:2013-06-10 11:40:26

标签: android android-listview listviewitem

我有一个扩展BaseAdapter的自定义列表视图。我希望按下按钮更新并显示textView / TextSwitcher的值。

我的代码

private class myListAdapter extends BaseAdapter implements ViewFactory {
    Context mcontext;
    final int[] bgColors = new int[] { R.drawable.bg_even,
            R.drawable.bg_odd };
    private ArrayList<HashMap<String, Object>> Books;

    public myListAdapter(ArrayList<HashMap<String, Object>> arrayList,
            Context context) {
        Books = arrayList;
        mcontext=context;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return Books.size();
    }

    public Object getItem(int position) {
        return Books.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("null")
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        try {
            ViewHolder holder = null;

            if (convertView == null) {
                try {
                    convertView = mInflater.inflate(
                            R.layout.addtocart_inflate, null);
                    int colorPosition = position % bgColors.length;
                    convertView
                            .setBackgroundResource(bgColors[colorPosition]);

                    convertView.setClickable(true);
                    OnClickListener clickListener = null;
                    convertView.setOnClickListener(clickListener);
                    holder = new ViewHolder();
                    holder.tv1 = (TextView) convertView.findViewById(R.id.TextView03);
                    holder.decrease = (ImageView) convertView.findViewById(R.id.menu_decrease_order);
                    holder.increase = (ImageView) convertView.findViewById(R.id.menu_increase_order);


                    holder.mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
                    holder.mSwitcher.setFactory(this);
                    Animation in = AnimationUtils.loadAnimation(mcontext,
                            android.R.anim.fade_in);
                    Animation out = AnimationUtils.loadAnimation(mcontext,
                            android.R.anim.fade_out);
                    holder.mSwitcher.setInAnimation(in);
                    holder.mSwitcher.setOutAnimation(out);
                    convertView.setTag(holder);
                } catch (Exception ex) {
                }
            } else {
                holder = (ViewHolder) convertView.getTag();
                int colorPosition = position % bgColors.length;
                convertView.setBackgroundResource(bgColors[colorPosition]);
            }
            System.out.println("Name is " +Books.get(position).get("name"));
            holder.tv1.setText((String) Books.get(position).get("name"));

            holder.increase.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    mCounter++; 
                    System.out.println("value is " +mCounter);
                    String temp= Integer.toString(mCounter);
                    holder.mSwitcher.setText(temp); //Can't do this here as it says Change Holder to final 
                }
            });

        holder.decrease.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                    if(mCounter==0){

                    }else{
                        mCounter--;             
                        System.out.println("value is " +mCounter);

                    }
                }
            });


        } catch (Exception ex) {
            CommonUtils.postException(ex, ex.getMessage());
        }

        return convertView;
    }

    class ViewHolder {
        TextView tv1;
        ImageView increase,decrease;
        TextSwitcher mSwitcher;

    }

    public View makeView() {
        TextView t = new TextView(mcontext);
        t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        t.setTextSize(36);
        return t;
    }
}

我想在按下按钮时增加或减少数量并在屏幕上显示。我知道通过在每次值更改时重新加载listView值来解决问题的一种方法。

但我正在寻找的是

我需要在运行时在textView中显示值而不重新加载值。在上面的代码中“它说修改器持有者更改为最终”如何在不重新加载ListView的情况下执行此操作。

1 个答案:

答案 0 :(得分:0)

    public int getCount() 
    {
            return Books.size();
    }

在此代码中,将Books.size()替换为arrayList.size(),因为在您的主要活动中,您正在更改数组列表的大小而不是书籍大小&amp;记住构造函数只被调用一次,这就是为什么书籍大小重复相同。

并且不要忘记,添加adapter.notifyDataSetChanged()(在您的班级中)