【问题标题】:listView with two buttons (Android)带有两个按钮的 listView (Android)
【发布时间】:2013-11-22 15:35:04
【问题描述】:

我对 ListView 有一个大问题。我在每个 listView 的行中有 2 个按钮,btPlus 和 btMinus(以及 4 个 TextView)。这两个按钮增加 (btPlus) 和减少 (btMinus) 标签 lblQty 的值。 我创建了一个以这种方式设置的自定义 ArrayAdapter:

public class ProductsViewAdapter extends ArrayAdapter<HashMap<String, String>>{
private int resource;
private LayoutInflater inflater;
private char tag;
private Context context;

private ArrayList<HashMap<String, String>> prodotti;
private HashMap<String, String> prodotto;

RowHolder holder;

public ProductsViewAdapter(Context context, int resourceId,
        ArrayList<HashMap<String, String>> objects, char t) {
          super(context, resourceId, objects);
          this.resource = resourceId;
          this.context = context;
          this.prodotti = objects;
          this.tag = t;
         }

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

  if (row == null) {
       LayoutInflater inflater = ((Activity) context).getLayoutInflater();
       row = inflater.inflate(resource, parent, false);
       holder = new RowHolder();
       holder.tvPid = (TextView) row.findViewById(R.id.pid);
       holder.tvNome = (TextView) row.findViewById(R.id.name);
       holder.tvDescr = (TextView) row.findViewById(R.id.description);
       holder.tvPrezzo = (TextView) row.findViewById(R.id.price);
       holder.btPlus = (Button) row.findViewById(R.id.btnPlus);
       holder.btMinus = (Button) row.findViewById(R.id.btnMinus);
       holder.tvQuantita = (TextView) row.findViewById(R.id.lblQty);
       row.setTag(holder);
  } 
  else {
       holder = (RowHolder) row.getTag();
  }
  prodotto = prodotti.get(position);
  if (tag == 'p'){
      holder.tvPid.setText(prodotto.get("idProdotto"));  
  }
  else {
      holder.tvPid.setText(prodotto.get("idBibita"));
  }

  holder.tvNome.setText(prodotto.get("nome"));
  holder.tvDescr.setText(prodotto.get("descrizione"));
  holder.tvPrezzo.setText(prodotto.get("prezzo"));
  holder.btPlus.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("Plus Button Clicked", "**********");
        Toast.makeText(context, "Plus button Clicked",
          Toast.LENGTH_LONG).show();
        int val = Integer.parseInt(holder.tvQuantita.getText().toString())+1;
        holder.tvQuantita.setText(String.valueOf(val));
       }
  });

  holder.btMinus.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("Minus Button Clicked", "**********");
        Toast.makeText(context, "Minus button Clicked",
          Toast.LENGTH_LONG).show();
        int val = Integer.parseInt(holder.tvQuantita.getText().toString());
        if (val > 0 ){
            val--;
            holder.tvQuantita.setText(String.valueOf(val));
        }
       }
  });
  return row;
 }

 static class RowHolder {
      TextView tvPid;
      TextView tvNome;
      TextView tvDescr;
      TextView tvPrezzo;
      TextView tvQuantita;
      Button btPlus;
      Button btMinus;
 }
}

所以我在我的活动中使用它:

ProductsViewAdapter adapter = new ProductsViewAdapter(
            AllProducts_View.this, R.layout.list_item_from_cat, productsList, 'p'
            );
            /*AllProducts_View.this, productsList,
            R.layout.list_item_from_cat, new String[] { TAG_PID,
                    TAG_NAME, TAG_DESCRIZIONE, TAG_PREZZO},
            new int[] { R.id.pid, R.id.name, R.id.description, R.id.price});*/
    // updating listview
    lv.setItemsCanFocus(false);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String, String> p = (HashMap<String, String>) parent.getItemAtPosition(position);
                Toast.makeText(
                                view.getContext(),
                                "Click sulla riga " + p.get("nome") + " " + p.get("prezzo"),
                                Toast.LENGTH_LONG
                                ).show();
        }
    });

这里是单行的xml代码(list_item_from_cat.xml):

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->

    <TextView
        android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <!-- Name Label -->

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textSize="24dp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textSize="20dp"
        android:textStyle="italic" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/price"
            android:layout_width="108dp"
            android:layout_height="34dp"
            android:layout_marginRight="53dp"
            android:layout_weight="1.34"
            android:text="TextView" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >


            <Button
                android:id="@+id/btnPlus"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="35dp"
                android:layout_height="wrap_content"
                android:text="+" 
                android:focusable="false"
        android:focusableInTouchMode="false"/>

            <TextView
                android:id="@+id/lblQty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:width="35dp" />



            <Button
                android:id="@+id/btnMinus"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="-"
                android:width="35dp"
                android:focusable="false"
                android:focusableInTouchMode="false" />

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

我的问题是,当我单击每一行中的按钮时,它们会增加或减少另一行的值。即使我用可变结果滑动 listView 也会发生这种情况。

谁能帮帮我?

非常感谢您的回答

【问题讨论】:

    标签: button android-listview


    【解决方案1】:

    这已经很老了,我假设您已经找到了答案,但以防万一... 看起来您所看到的是细胞回收器在运行的结果。您正在直接设置 textview 的值,这意味着每第 n 行都会受到更新的影响。 例如,假设您的列表视图显示第 0、1、2 和 3 行。如果我理解您的意思,您添加到第 0 行,然后当您开始滚动时,第 4 行也发生了变化。同样,如果您向下滚动到大概 7,它也会更改,以及此后的每 4 个项目。

    您可以通过在与列表关联的对象中设置新数量值然后从该对象设置文本字段来解决此问题。而不是:

    int val = Integer.parseInt(holder.tvQuantita.getText().toString())+1;
    holder.tvQuantita.setText(String.valueOf(val));
    

    做:

    int val = //get value from list object and increment by 1
    object.setValue(val); //put the value back into the object
    holder.tvQuantita.setText(String.valueOf(object.getValue());
    

    从您的代码的外观来看,您可能需要进行一些重构才能到达那里,但这应该不会太糟糕。你有一个 HashMap。您可以将其重构为

    HashMap<String, CompoundObject> 
    

    复合对象的样子:

    public class Container {
        private String currentString;
        private int quantity;
        //don't forget getters and setters, and maybe equals/hashCode
    }
    

    然后您就可以在不更改所有数量的情况下设置新值。 最后,您需要在设置名称、描述和价格的每个 getView 上设置此对象的值。

    holder.tvNome.setText(prodotto.get("nome"));
    holder.tvDescr.setText(prodotto.get("descrizione"));
    holder.tvPrezzo.setText(prodotto.get("prezzo"));
    holder.tvQuantita.setText(/*set quantity here*/);
    holder.btPlus.setOnClickListener(new OnClickListener() {
    

    这应该使您的 ListViews 不会重复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 2012-10-19
      相关资源
      最近更新 更多