Reputation: 73
Hi hoping someone can help. I have a list view that displays various textviews,editviews and abutton on each row. I have used the 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");
I then add a OnClickListner for the add button with in the getView
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();
}
});
The button text changes fine on the button i clicked on which is great. The problem im having is that if i scroll down every now and then i see the text has also changed on one of the buttons on the screen.
Can anyone help?
Thanks Neil
XML for the list view
<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>
Upvotes: 0
Views: 973
Reputation: 67286
You have to store the text of button in some kind of collection. As you can using HashMap here you can create one more Object of HashMap that will store the text of Button inside getView()
using
bAdd.setText(((HashMap<String,String>) getItem(position)).get("btn_text"));
then you can setTag()
the instance to use in onClick()
bAdd.setTag((HashMap<String,String>) getItem(position));
And then inside onClick()
use getTag()
to get the position of the 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"));
}
Upvotes: 2
Reputation: 4191
How do you initialize your bAdd button...nevermind I see it now.
I would check your XML file, two of your buttons probably have the same id.
Upvotes: 0