使用listview上的simpleadapter单击列表视图中的增量Textview值

时间:2016-11-14 14:05:50

标签: android listview simpleadapter

我正在处理一个片段,它包含listviews,我想通过点击listitem行来更新数量值/

这是我的代码

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

    }


}

我想更新每行列表视图中显示的项目数量。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

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


            }
        });

my_counter替换为您的计数器TextView's ID。