如何获取列表视图中的所有TextView的价值在android系统? [英] How to get all the TextView value in listview in android?

查看:189
本文介绍了如何获取列表视图中的所有TextView的价值在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得在列表视图中的所有TextView的价值。

I am trying to get all the TextView value in listview.

我有N无的列表项一个ListView。

I have a ListView with N no's of list items.

所有列表项增加拥有和增减键。

All the list items have Increase and Decreas button.

如果我按一下按钮增加,总数(TextView的)价值将得到增量。如果我点击减小按钮,总数(TextView中)将得到降低。

If I click Increase button, total count(TextView) value will get increment. If i click Decrease button, total count(TextView) will get decreased.

现在我想从列表中得到所有的总数值。

Now I want to get all the total count values from the list.

下面是我的code: -

Here is my code:-

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的值。

Please let me any idea to get all the TextView values in list view.

。由此我重视我的整个code:

Herewith I have attached my entire code:

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

推荐答案

所以,最后我找到了解决办法: -

So, finally I found the solution:-

在我的 ListViewExample 类我已经添加下面的方法。

In my ListViewExample class I have added the following methods.

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

这篇关于如何获取列表视图中的所有TextView的价值在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆