android-自定义listview与edittext

时间:2011-12-29 08:09:09

标签: android

我有一个自定义listView,每行都有editText来携带用户输入的值。 我在listview旁边有一个按钮,onclick将从列表中的所有行中添加editText值。

任何人都可以给我一些链接以获取一些线索。

import android.app.Activity;
import android.app.ListFragment;
import android.content.PeriodicSync;
import android.os.Bundle;

import android.text.InputFilter.LengthFilter;
import android.util.Log;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

    public class NRSessionScheduleFragmentOne extends ListFragment {

        String[] dummyVal = new String[] { "hello", "how", "are", "u", "how",
                "are", "u", "how", "are" };
        ListView parentList;
        int addVal;
        EditText period;

        private NRSessionFragmentHolder parentobj;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View v = inflater.inflate(R.layout.nrtpsessiontopics, container, false);
            return v;
        }

        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new MyListAdapter(getActivity()));
            parentList = (ListView) getActivity().findViewById(android.R.id.list);
            addVal = valueReturn(dummyVal, period, parentList);
        }

        public class MyListAdapter extends ArrayAdapter {
            Activity contextACT;

            public MyListAdapter(Activity context) {
                super(context, R.layout.nrrow_of_list_lesson, dummyVal);
                this.contextACT = context;
            }

            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                LayoutInflater inflator = contextACT.getLayoutInflater();
                final View row = inflator.inflate(R.layout.nrrow_of_list_lesson,
                        null);
                final TextView name = (TextView) row.findViewById(R.id.lessonname);
                period = (EditText) row.findViewById(R.id.ssperiodeditText);
                final Button increase = (Button) row.findViewById(R.id.ssincrease);
                final Button decrease = (Button) row.findViewById(R.id.ssdecrease);
                increase.setId(position);
                decrease.setId((dummyVal.length) + position);

                final Integer currentQuant = Integer.parseInt(period.getText()
                        .toString());

                increase.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        increase.getId();
                        Log.w("button value", "" + increase.getId());
                        if (currentQuant != 0) {
                            period.setText(new Integer(currentQuant + 1).toString());
                        }

                    }
                });

            decrease.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Log.w("button value1", " " + decrease.getId());
                    decrease.getId();
                    if (currentQuant != 1) {
                        period.setText(new Integer(currentQuant - 1).toString());
                    }

                }
            });


            return (row);

        }

    }

    void setParent(NRSessionFragmentHolder parent) {
        this.parentobj = parent;
    }

    public int valueReturn(String[] dummyVal, EditText period2,
            ListView parentList) {
        Integer add = 0;
        for (int i = 0; i <= dummyVal.length; i++) {
            View temp = null;
            temp = parentList.getChildAt(i);
            while (!(temp == null)) {
                add = Integer.parseInt(period2.getText().toString());

            }
        }
        return add;
    }

}

2 个答案:

答案 0 :(得分:1)

嘿我找到了一种方法来做到这一点......事实证明这很简单....这里是代码....这里我使用ListView来代替值....

 parentList=(ListView)findViewByid(R.id.list);

 public int getTvValue() {
    View view;
    String[] edVal = new String[parentList.getChildCount()];
    int val = 0;
    for (int i = 0; i < parentList.getCount(); i++) {
        view = parentList.getChildAt(i);
        TextView tv = (TextView) view.findViewById(R.id.TVmid);
        edVal[i] = tv.getText().toString();
        val = Integer.parseInt(edVal[i]) + val;
    }
    return val;

}

答案 1 :(得分:0)

你需要你的Button onClickListener()来获取listView的适配器,将它转换为List并总结列表的内容。