Reputation: 87
I have made a listview with the following items on each row
The 3rd textview is between two buttons(button will increase and decrease the value shown in the textview).when I am increasing and decreasing the value of textview of first row and then I go to second row it is taking the previous value when I increase and decrease its value. I am using a counter and incrementing the counter by fixed amount on click of button.
Issue Faced:
I have used adapter class.
How to resolve this issue as I am new to android. Tell me way to do this.
This is my adapter class:
public class CartListViewAdapter extends BaseAdapter{
private Context mcontext;
private static int counter;
private String stringVal;
//private static int i = position;
public CartListViewAdapter(Context c){
mcontext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length ;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View myView = convertView;
//if (convertView == null) { // if it's not recycled, initialize some attributes
//Inflate the layout
LayoutInflater inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.row, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.image);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.text);
tv.setText(names[position] );
final TextView tv1 = (TextView)myView.findViewById(R.id.text1);
tv1.setText(names1[position]);
TextView tv3 = (TextView)myView.findViewById(R.id.text3);
tv3.setText(names3[position]);
ImageButton button = (ImageButton)myView.findViewById(R.id.addbutton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(mcontext,"Button is clicked",Toast.LENGTH_SHORT).show();
// counter+=250;
// stringVal = Integer.toString(counter);
// tv1.setText(stringVal);
for(int position=0;position<mThumbIds.length;position++){
counter = 250;
counter+=250;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
}
}
});
ImageButton button2= (ImageButton)myView.findViewById(R.id.subbutton);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(mcontext,"Button is clicked",Toast.LENGTH_SHORT).show();
for(int position=0;position<mThumbIds.length;position++){
counter = 250;
counter-=250;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
}
}
});
return myView;
}
private Integer[] mThumbIds = {
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
};
private String[] names={"ab","cd","ef"};
private String[] names1 = {"250","250","250"};
private String[] names3 = {"yhhjd","hnbnn","fdffd"};
}
This is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="vertical"
android:padding="10sp" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp" />
</LinearLayout>
<ImageButton
android:id="@+id/addbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50sp"
android:layout_marginTop="20sp"
android:background="@android:color/background_light"
android:src="@android:drawable/ic_input_add" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3sp"
android:layout_marginTop="20sp" />
<ImageButton
android:id="@+id/subbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:background="@android:color/background_light"
android:src="@android:drawable/ic_input_add" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10sp"
android:layout_marginTop="20sp"
android:layout_weight="0.3"
android:gravity="right" />
</LinearLayout>
Thanks.
Upvotes: 2
Views: 8950
Reputation: 2614
Try this:
CartListViewAdapter myAdapter = ((AdapterView)v.getParent()).getAdapter();
for(int i=0;i<myAdapter.getCount();i++) {
counter = Integer.parseInt(names1[i]);
counter -= 250;
names[i] = String.valueOf(counter);
}
myAdapter.notifiyDataSetChanged();
I believe this would work, although be ready to fix some code.
Upvotes: 1
Reputation: 1532
I would like to give my suggestion as solution.
While dealing with list view items and in a situation to update the list view items, you have to update the content of the source values (ie: the arraylist ).
Update the values in the arraylist and Notify the adapter that the data set has been changed. as NotifyDataSetChanged()
NotifyDataSetChanged()
Upvotes: 0
Reputation: 768
Do you want to decrease or increase of each textView's value? If yes
for(int position=0;position<mThumbIds.length;position++){
counter = Integer.parseInt(tv1.getText());
counter-=250;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
}
Upvotes: 1