2016-11-14 1 views
0

я работаю над фрагментом и содержит ListViews и я хочу сделать обновление значения количества, нажав на ListItem строке/значение Incremetn TextView в ListView с помощью simpleadapter на ListView нажмите

вот мой код

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

    } 


} 

Я хочу обновить количество элементов, отображаемых в списке в каждой строке.

ответ

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 ид.

Смежные вопросы