如何获取android中listview中的所有TextView值?

时间:2015-02-10 05:20:42

标签: android listview

我想在listview中获取所有TextView值。

我有一个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

2 个答案:

答案 0 :(得分:4)

所以,最后我找到了解决方案: -

在我的ListViewExample课程中,我添加了以下方法。

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

答案 1 :(得分:0)

我希望这能回答您的问题:

float s=0;

for(int i=0;i<rw.getCount();i++) {
    View view = rw.getAdapter().getView(0, null, rw);
    TextView name = (TextView) view.findViewById(R.id.product_price);
    s=s+Float.parseFloat(name.getText().toString());
}
total.setText("Total:"+String.valueOf(s));