2015-02-10 3 views
0

Я пытаюсь получить все значение TextView в listview.Как получить все значение TextView в listview в android?

У меня есть ListView с N элементами списка.

Все элементы списка имеют кнопку увеличения и уменьшения.

Если я нажму кнопку «Увеличить», общее значение счетчика (TextView) получит инкремент. Если я нажму кнопку «Уменьшить», общее количество (TextView) будет уменьшено.

Теперь я хочу получить все общие значения счета из списка.

Вот мой код: -

holder.button.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        View parentView = (View) v.getParent(); 

        String getString = ((TextView) parentView.findViewById(R.id.counter)).getText().toString(); 

        String totalAmtString = ((TextView) parentView 
          .findViewById(R.id.text1)).getText().toString(); 

        int totAmount = Integer.parseInt(totalAmtString); 

        count = Integer.parseInt(getString); 
        count++; 
        tot_amt = totAmount * count; 

        countString = String.valueOf(count); 

        String TotAmt = String.valueOf(tot_amt); 

        ((TextView) parentView.findViewById(R.id.counter)) 
          .setText(countString); 

        ((TextView) parentView.findViewById(R.id.totalPrice)).setText(TotAmt); 

        for(int i=0;i>data.size();i++){ 
         // Here I want to get the all TotAmt every time i click Increase button. 
        } 

       } 

      }); 

Пожалуйста, дайте мне любую идею, чтобы получить все значения TextView в виде списка.

Настоящим я приложил весь свой код:

public class ListViewExample extends Activity { 
    ListView list; 
    CustomAdapter adapter; 
    public ListViewExample CustomListView = null; 
    public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>(); 
    New_SQLController sqlcon; 

    static TextView totalAmout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.database_list); 

     sqlcon = new New_SQLController(this); 
     InsertToDB(); 
     BuildTable(); 
     CustomListView = this; 


     Resources res = getResources(); 

     list = (ListView) findViewById(R.id.list); 

     totalAmout=(TextView)findViewById(R.id.textView1); 

     adapter = new CustomAdapter(CustomListView, CustomListViewValuesArr, 
       res); 

     list.setAdapter(adapter); 

    } 

    private void InsertToDB(){ 
     sqlcon.open(); 
     sqlcon.insertToProductTable("IMAGE","Idly","20","2","MorAndEve"); 
     sqlcon.insertToProductTable("IMAGE","Doasi","40","1","MorAndEve"); 
     sqlcon.insertToProductTable("IMAGE","Poori","30","1","Mor"); 
     sqlcon.close(); 
    } 

    private void BuildTable() { 

     sqlcon.open(); 
     Cursor c = sqlcon.readEntry(); 

     int rows = c.getCount(); 
     int cols = c.getColumnCount(); 

     c.moveToFirst(); 


     for (int i = 0; i < rows; i++) { 

      final ListModel sched = new ListModel(); 
      sched.setProductName(c.getString(1)); 
      sched.setPrice(c.getString(2)); 
      sched.setQuantity(c.getString(3)); 
      CustomListViewValuesArr.add(sched);/* 

      for (int j = 0; j < cols; j++) { 
       System.out.println("c.getString(" + j + ")" + c.getString(j)); 

      }*/ 
      c.moveToNext(); 
     } 
     sqlcon.close(); 
    } 

    public void onItemClick(int mPosition) { 
     ListModel tempValues = (ListModel) CustomListViewValuesArr 
       .get(mPosition); 
     Toast.makeText(
       CustomListView, 
       "\n\n User Id:-" + tempValues.getProductName() + "\n\n Image:" 
         + tempValues.getPrice(), Toast.LENGTH_LONG).show(); 
    } 

    public static void setTotalAmount(String TotalAmount){ 
     totalAmout.setText(TotalAmount); 
    } 


} 

Мой адаптер класса

public class CustomAdapter extends BaseAdapter implements OnClickListener { 

    private Activity activity; 
    private ArrayList data; 
    private static LayoutInflater inflater = null; 
    public Resources res; 
    ListModel tempValues = null; 
    ListModel tempValuesNew = null; 

    int i = 0; 
    static int count = 0; 
    static int tot_amt = 0; 
    String countString = "0"; 
    ImageButton btn; 
    ViewHolder holder; 

    static int OverAllTotal = 0; 
    String overAllString = "0"; 

    int pos; 

    public CustomAdapter(Activity a, ArrayList d, Resources resLocal) { 

     activity = a; 
     data = d; 
     res = resLocal; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 

     if (data.size() <= 0) 
      return 1; 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public static class ViewHolder { 

     public TextView text; 
     public TextView text1; 
     public TextView textWide; 
     public TextView tvCounter; 

     public ImageView image; 
     ImageButton button; 
     ImageButton decreesButton2; 

    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     View vi = convertView; 
     pos = position; 
     if (convertView == null) { 

      vi = inflater.inflate(R.layout.tabitem, null); 
      btn = (ImageButton) vi.findViewById(R.id.button1); 

      holder = new ViewHolder(); 
      holder.text = (TextView) vi.findViewById(R.id.text); 
      holder.text1 = (TextView) vi.findViewById(R.id.text1); 
      holder.image = (ImageView) vi.findViewById(R.id.list_image); 
      holder.tvCounter = (TextView) vi.findViewById(R.id.counter); 
      holder.button = (ImageButton) vi.findViewById(R.id.button1); 
      holder.decreesButton2 = (ImageButton) vi.findViewById(R.id.button2); 

      vi.setTag(holder); 
     } else 
      holder = (ViewHolder) vi.getTag(); 

     if (data.size() <= 0) { 
      holder.text.setText("No Data"); 

     } else { 

      tempValues = null; 
      tempValues = (ListModel) data.get(position); 


      holder.text.setText(tempValues.getProductName()); 
      holder.text1.setText(tempValues.getPrice()); 
      holder.image.setImageResource(res.getIdentifier(
        "com.list.listviewexample:drawable/" 
          + tempValues.getImage(), null, null)); 


      vi.setOnClickListener(new OnItemClickListener(position)); 

      holder.button.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        View parentView = (View) v.getParent(); 

        String getString = ((TextView) parentView.findViewById(R.id.counter)).getText().toString(); 

        String totalAmtString = ((TextView) parentView 
          .findViewById(R.id.text1)).getText().toString(); 

        int totAmount = Integer.parseInt(totalAmtString); 

        count = Integer.parseInt(getString); 
        count++; 
        tot_amt = totAmount * count; 

        countString = String.valueOf(count); 

        String TotAmt = String.valueOf(tot_amt); 

        ((TextView) parentView.findViewById(R.id.counter)) 
          .setText(countString); 

        ((TextView) parentView.findViewById(R.id.totalPrice)).setText(TotAmt); 

        int TempTotAmt = totAmount * count; 
        OverAllTotal = OverAllTotal + tot_amt; 

        overAllString = String.valueOf(OverAllTotal); 
        ListViewExample.setTotalAmount(overAllString); 

        for(int i=0;i>data.size();i++){ 

        } 

       } 

      }); 

      holder.decreesButton2.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        View parentView = (View) v.getParent(); 
        String getString = ((TextView) parentView 
          .findViewById(R.id.counter)).getText().toString(); 

        String totalAmtString = ((TextView) parentView 
          .findViewById(R.id.text1)).getText().toString(); 

        int totAmount = Integer.parseInt(totalAmtString); 

        count = Integer.parseInt(getString); 
        if (count > 0) 
         count--; 
        countString = String.valueOf(count); 
        tot_amt = totAmount * count; 

        String TotAmt = String.valueOf(tot_amt); 

        ((TextView) parentView.findViewById(R.id.totalPrice)) 
          .setText(TotAmt); 
        ((TextView) parentView.findViewById(R.id.counter)) 
          .setText(countString); 
       } 

      }); 
     } 
     return vi; 
    } 

    @Override 
    public void onClick(View v) { 

    } 

    private class OnItemClickListener implements OnClickListener { 
     private int mPosition; 

     OnItemClickListener(int position) { 
      mPosition = position; 
     } 

     @Override 
     public void onClick(View arg0) { 

      ListViewExample sct = (ListViewExample) activity; 
      sct.onItemClick(mPosition); 
     } 
    } 
}` 

Screen sample

+0

Что не так с методом getCount(), который вы переопределили? Кроме того, чтобы получить все текстовые данные, переместите переменную 'data'. – MeetTitan

+0

Я. Я не знаю, где мне поставить петлю. Пожалуйста, укажите любые образцы. –

+0

Где вы хотите данные? По щелчку? – MeetTitan

ответ

4

Итак, наконец-то я нашел решение: -

В моей ListViewExample класс I добавил следующие методы.

public void getAllValues() { 
     View parentView = null; 
     int temp = 0; 
     int getTotal = 0; 
     int totQuan = 0; 
     int quan = 0; 
     for (int i = 0; i < list.getCount(); i++) { 
      parentView = getViewByPosition(i, list); 

      String getString = ((TextView) parentView 
        .findViewById(R.id.totalPrice)).getText().toString(); 

      getTotal = Integer.parseInt(getString); 
      temp = temp + getTotal; 

      totQuan = Integer.parseInt(((TextView) parentView 
        .findViewById(R.id.counter)).getText().toString()); 
      quan = quan + totQuan; 

     } 
     setTotalAmount(String.valueOf(temp), String.valueOf(quan)); 

    } 

    public View getViewByPosition(int pos, GridView listView) { 
     final int firstListItemPosition = listView.getFirstVisiblePosition(); 
     final int lastListItemPosition = firstListItemPosition 
       + listView.getChildCount() - 1; 

     if (pos < firstListItemPosition || pos > lastListItemPosition) { 
      return listView.getAdapter().getView(pos, null, listView); 
     } else { 
      final int childIndex = pos - firstListItemPosition; 
      return listView.getChildAt(childIndex); 
     } 
    } 
+0

'public View getViewByPosition (int pos, GridView listView)' Вы имели в виду 'public View getViewByPosition (int pos, ListView listView)'? Он не подходит, поскольку ** list ** является ** ListView **, когда требуется параметр ** GridView ** – poring91

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