Nikhilkumar Waghaye
Nikhilkumar Waghaye

Reputation: 87

How to increment and decrement value of textview in listview?

I have made a listview with the following items on each row

  1. Image Icon.
  2. Button.
  3. TextView
  4. Button.
  5. TextView.

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:

  1. I am initializing counter but as there many rows in the listview its taking the previous value as initial value for second row.How to refresh the counter value?
  2. How to restore the initial value of textview to its original value?

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

Answers (3)

nstosic
nstosic

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

Tamilselvan Kalimuthu
Tamilselvan Kalimuthu

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()

  • first of all in the adapter setTag() the position to that list view item(ie:textView) that u r performing the onClick.
  • Then, in the onclick method getTag() the position and get the respective hashmap or object from the arrayList as arrayList.get(position);
  • Then update the Hashmap or object with the new Value. and use NotifyDataSetChanged()

Upvotes: 0

Daryn
Daryn

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

Related Questions