0

您好希望有人可以提供帮助。我有一个列表视图,在每一行上显示各种文本视图、编辑视图和按钮。我用过getView

     public View getView(final int position, View convertView, ViewGroup parent) 
    {            
        View v = convertView;


           if (v == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = inflater.inflate(R.layout.catlistrow, null);

           }

           TextView pDesc = (TextView) v.findViewById(R.id.productlinerow);
           TextView pPack = (TextView) v.findViewById(R.id.productlinerow3);
           ImageView iThumbnail = (ImageView) v.findViewById(R.id.Thumbnail);
           final Button bAdd = (Button)v.findViewById(R.id.SOCatLine);

            final EditText pOrdQty = (EditText) v.findViewById(R.id.SOQty);


           pDesc.setText(((HashMap<String,String>) getItem(position)).get("Column2")); 
           pPack.setText(((HashMap<String,String>) getItem(position)).get("Column3"));
           pOrdQty.setText(((HashMap<String,String>) getItem(position)).get("OrdQty"));
           String EanCode = ((HashMap<String,String>) getItem(position)).get("EANCode");

然后我在 getView 中为添加按钮添加一个 OnClickListner

                bAdd.setOnClickListener(new View.OnClickListener(){          
        public void onClick(View v) {


            Object o = list.get(position);
            HashMap<String, String> map2 = (HashMap<String, String>)o;
            String b = map2.get("OrdQty");
            String sProdCode = map2.get("ProdCode");
            String ProdPrice = map2.get("ProdPrice");
            b = pOrdQty.getText().toString();

            int OrderQty = Integer.parseInt(b);

            //Check to see if add is pressed and qty zero
            if ( OrderQty == 0 )
            {
              OrderQty = 1;
              b = "1";

            }

            db.open();
            int iOrderNo = Integer.parseInt(OrderNo);


            // update line
            iLineNumber = iLineNumber + 1;
            int QtyCheck = db.createCATorderline(iOrderNo, iLineNumber,
                            sProdCode, ProdPrice, b,"P");


            bAdd.setText("Change"); 


            //Close the database
            db.close();



        }   

  }); 

我单击的按钮上的按钮文本变化很好,这很棒。我遇到的问题是,如果我不时向下滚动,我会看到屏幕上的一个按钮上的文本也发生了变化。

任何人都可以帮忙吗?

谢谢尼尔

列表视图的 XML

 <ImageView
android:id="@+id/Thumbnail"
android:layout_width="190px"
android:layout_height="200px"
android:layout_alignParentLeft="true"
android:maxWidth="190px"
android:maxHeight="200px"
android:background="@null" />

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/productlinerow"
 android:textColor="#000000" 
 android:layout_marginTop="10dip"
 android:textSize="25dip"
 android:layout_marginBottom="20dip"
 android:layout_height="match_parent" 
 android:text="column1"
 android:layout_width="400dip" 
 android:layout_toRightOf="@+id/Thumbnail"/> 


<TextView android:id="@+id/productlinerow3" 
 android:textColor="#000000" 
 android:textSize="25dip"
 android:text="column2" 
 android:layout_marginTop="10dip"
 android:layout_height="match_parent"  
 android:layout_width="300dip" 
 android:layout_below="@+id/productlinerow"
 android:layout_toRightOf="@+id/Thumbnail"/>

<TextView
      android:id="@+id/qtylabel"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_below="@+id/productlinerow"
      android:layout_toLeftOf="@+id/SOQty"
      android:layout_marginTop="40dip"
      android:layout_marginRight="30dip" 
      android:inputType="number" 
      android:editable="true"
      android:gravity="right" 

      android:text="Order Qty:"
      android:textSize="25dip" />

  <EditText
       android:id="@+id/SOQty"
       android:layout_width="100dip"
       android:layout_height="60dip"
       android:layout_marginTop="25dip"
       android:layout_toLeftOf="@+id/SOCatLine"
       android:layout_marginRight="30dip"
       android:layout_below="@+id/productlinerow"
       android:text="1"
       android:maxLength="3"
       android:inputType="number"
       android:singleLine="true"
       android:textSize="25dip" 
       android:imeOptions="flagNoExtractUi"
       android:selectAllOnFocus="true"/>

 <Button android:text="Add" android:background="@drawable/green_button"  

 android:id="@+id/SOCatLine"
 android:textSize="25dip" 
 android:textColor="#FFFFFF" 
 android:layout_width="130dip" 
 android:layout_marginTop="30dip" 
 android:layout_height="wrap_content" 
 android:layout_below="@+id/productlinerow"
 android:layout_alignParentRight="true"
  />



</RelativeLayout>
4

2 回答 2

2

您必须将按钮的文本存储在某种集合中。正如您可以在此处使用 HashMap 一样,您可以再创建一个 HashMap 对象,该对象将使用以下方法存储 Button 的getView()文本

bAdd.setText(((HashMap<String,String>) getItem(position)).get("btn_text"));

然后你可以setTag()在实例中使用onClick()

bAdd.setTag((HashMap<String,String>) getItem(position));

然后在里面onClick()使用getTag()来获取Button的位置。

@Override
        public void onClick(View v) {
        HashMap<String,String> map = (HashMap<String,String>)v.getTag();
        map.put("btn_text", "Change");
        ((Button)v).setText(map.get("btn_text"));
}
于 2012-07-11T13:00:45.143 回答
0

你如何初始化你的 bAdd 按钮……没关系,我现在看到了。

我会检查您的 XML 文件,您的两个按钮可能具有相同的 ID。

于 2012-07-11T11:36:29.373 回答