0

我有一个扩展 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 的情况下执行此操作。

4

1 回答 1

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

在此代码中,替换Books.size()arrayList.size(),因为在您的主要活动中,您正在更改数组列表的大小而不是 Books 大小并且记住构造函数只调用一次,这就是 Books 大小保持不变的原因。

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

于 2013-06-10T11:49:04.127 回答