3

我有一个使用自定义 listAdapter 设置的列表视图。每个 listView 项目中都有一个按钮来更改同一行中的 textView。我怎样才能做到?这是代码的一部分:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    final int _position = position;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.activity_today_sales_list_view, null);
        holder = new ViewHolder();
        holder.txt_itemName = (TextView) convertView.findViewById(R.id.itemNameTB);
        holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.priceTB);
        holder.txt_plusBtn = (Button) convertView.findViewById(R.id.addBtn);
        holder.txt_minusBtn = (Button) convertView.findViewById(R.id.minusBtn);


        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt_plusBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

             Log.d("Plus Btn", "Clicked");

        }


    });

    holder.txt_minusBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.d("Minus Btn", "Clicked");

        }
    });

    holder.txt_itemName.setText(itemDetailsrrayList.get(position).getItemName());
    holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getItemCost());

    return convertView;
}
4

4 回答 4

10

您可以使用以下代码来解决您的问题。

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.activity_today_sales_list_view, null);
        holder = new ViewHolder();
        holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.priceTB);
        holder.txt_plusBtn = (Button) convertView.findViewById(R.id.addBtn);
        holder.txt_plusBtn.setOnClickListener(new View.OnClickListener() {

             @Override
            public void onClick(View v) {
                ViewHolder holder1 = (ViewHolder)v.getTag();
                //Access the Textview from holder1 like below
                holder1.txt_itemPrice.setText("Plus");

            }
        });

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.txt_plusBtn.setTag(holder);

    holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getItemCost());

    return convertView;
}
于 2013-05-08T10:09:10.130 回答
0

这是我自己的示例,说明了如何onClickListenerAdapter. 功能几乎和你一样。我有一个产品列表,在我的列表项中,用户可以增加或减少购买的物品的数量。

我的项目对象持有项目属性。它有一个 ID、一个名称和一个订单,表示该商品的购买次数:

public class MenuItem {
    private int id;
    private String name;
    private int order = 0;

    public MenuItem(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

}

这是我的一个片段,Adapter其中包含一个 MenuItems 列表。UI 包含一个向上和向下图像按钮以及名称。

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

        if (convertView == null) {
            LayoutInflater lInflater = ((Activity) mContext).getLayoutInflater();
            convertView = lInflater.inflate(R.layout.menu_item, parent, false);

            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.menu_name);
            holder.details = (TextView) convertView.findViewById(R.id.menu_details);
            holder.counter = (TextView) convertView.findViewById(R.id.menu_counter);
            holder.up = (ImageView) convertView.findViewById(R.id.menu_up);
            holder.down = (ImageView) convertView.findViewById(R.id.menu_down);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(getMenuList().get(position).getName());
        holder.details.setText(getMenuList().get(position).getDetails());
        holder.counter.setText(String.valueOf(getMenuList().get(position).getOrder()));

        holder.up.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onListItemClick(position, UP);
            }
        });

        holder.down.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                onListItemClick(position, DOWN);
            }
        });
        return convertView;
    }

    protected void onListItemClick(int position, int direction) {
        int numberOfOrders = getMenuList().get(position).getOrder();
        if (direction == DOWN && numberOfOrders == 0) {
            // Don't go negative!
            return;
        }

        getMenuList().get(position).setOrder(numberOfOrders +  direction);
            // in your case: itemDetailsrrayList.get(position).setOrder(...);
        notifyDataSetChanged();
    }

玩得开心!

于 2013-05-08T10:01:46.283 回答
0

尝试这个

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    final int _position = position;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.activity_today_sales_list_view, null);
        holder = new ViewHolder();
        holder.txt_itemName = (TextView) convertView.findViewById(R.id.itemNameTB);
        holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.priceTB);
        holder.txt_plusBtn = (Button) convertView.findViewById(R.id.addBtn);
        holder.txt_minusBtn = (Button) convertView.findViewById(R.id.minusBtn);


        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt_plusBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

                 Log.d("Plus Btn", "Clicked");
                 holder.txt_itemName.setText("Your String");
                 holder.txt_itemPrice.setText("Your String");

        }


    });

    holder.txt_minusBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.d("Minus Btn", "Clicked");
            holder.txt_itemName.setText("Your String");
            holder.txt_itemPrice.setText("Your String");

        }
    });

    holder.txt_itemName.setText(itemDetailsrrayList.get(position).getItemName());
    holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getItemCost());

    return convertView;
}

希望这可以帮助。

于 2013-05-08T09:40:50.913 回答
0

设置 Button 和 TextView 的 id,您可以在单击按钮时进行区分。我的意思是..假设您有 10 个项目要显示在列表视图中,因此getView()方法将接收position范围从 0 到 9。所以您可以在这里做的是设置 Buttons like10+position和 TextViews like 的id1000+position或任何随机数. 因此,当单击按钮时,您可以获得它的 id,从中减去 10。现在向其添加 1000 以获取 TextView 并更改其文本。

于 2013-05-08T09:43:31.677 回答