Addi.Star Addi.Star - 18 days ago 4
Android Question

Incremetn Textview value in listview using simpleadapter on listview click

i am working on a fragment and it contains listviews and i want do update the quantity value by clicking on listitem row/

here is my code

public class Fruites extends Fragment {

ImageButton b1,b2,b3,b4,b5;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View fruites = inflater.inflate(R.layout.fruites_frag, container, false);
//((TextView)android.findViewById(R.id.textView)).setText("Fruites");






perform(fruites);



return fruites;
}



ListView lv;
String[] values=new String[]{"Apple", "Banana", "oranges","Strawberries", "Grapes", "Gauva"};
int[] flags = new int[]{R.drawable.apple,R.drawable.ban,R.drawable.oranges,R.drawable.strawb,R.drawable.images,
R.drawable.gauv };
int inc = 0;
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

// Each row in the list stores country name, currency and flag


public void perform(View v) {

// List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

for(int i=0;i<5;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Name : " + values[i]);
hm.put("cur","Qty : " + inc); // i want to increment this value by clicking listview
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);

}

// Keys used in Hashmap
String[] from = { "flag","txt","cur" };

// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};

// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList, R.layout.list_row, from, to);

// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView )v.findViewById(R.id.listview);

//listView.bringToFront();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {






// here i want to implement to increment the variable by clicking the row items

Object o = parent.getItemAtPosition(position);
//String str=(String)o;

Toast.makeText(getActivity(),
"Button is clicked"+o, Toast.LENGTH_LONG).show();


}
});


// Setting the adapter to the listView
listView.setAdapter(adapter);

}


}


i want to update the quantity of item displayed in listview at each row .

Answer

You can do this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // view variable passed is a row
                TextView myCounter = (TextView)view.findViewById(R.id.my_counter);
                //Assuming it already has a int value
                int currentValue = Integer.parseInt(myCounter.getText().toString());
                myCounter.setText(String.valueOf(currentValue + 1));

                Object o = parent.getItemAtPosition(position);
                //String str=(String)o;

                Toast.makeText(getActivity(),
                        "Button is clicked"+o, Toast.LENGTH_LONG).show();


            }
        });

Replace my_counter with your counter TextView's id.

Comments