无法获取动态生成的textviews值

时间:2014-01-30 11:21:28

标签: android

我没有得到动态生成的TextViews的值。

我的代码是,

public class Stock_check_fragment extends Fragment {
    private AutoCompleteTextView auto;
    ArrayAdapter<String> adapter;
    ArrayList<String> values;
    LinearLayout ly;
    int i = 0;
    List<EditText> allEds = new ArrayList<EditText>();
    Button btn;
    EditText qty;
    Button next;

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

        View rootView = inflater
                .inflate(R.layout.stock_check, container, false);

        ly = (LinearLayout) rootView.findViewById(R.id.linearMain);

        auto = (AutoCompleteTextView) rootView
                .findViewById(R.id.autoCompleteTextView1);
        btn = (Button) rootView.findViewById(R.id.button1);
        next = (Button) rootView.findViewById(R.id.next);

        values = new ArrayList<String>();
        values.add("Kasun");
        values.add("KasunW");
        values.add("Sinmki");

        adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.select_dialog_item, values);

        auto.setThreshold(1);
        auto.setAdapter(adapter);

        auto.setOnItemClickListener(autoItemSelectedListner);
        next.setOnClickListener(o1);

        return rootView;

    }

    private OnItemClickListener autoItemSelectedListner = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Add_text(auto.getText().toString());
        }
    };

    public void Add_text(String value) {

        LinearLayout ll = new LinearLayout(getActivity());
        ll.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.setId(i);

        TextView product = new TextView(getActivity());
        product.setText(value);
        ll.addView(product);

        qty = new EditText(getActivity());
        qty.setText(i + "");
        qty.setId(i);
        qty.setWidth(120);

        ll.addView(qty);

        Button btn = new Button(getActivity());
        ll.addView(btn);
        btn.setLayoutParams(params);
      ly.addView(ll);
        i++;

    }



    OnClickListener o1 = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ViewGroup parentView = ly;

            for (int i = 0; i < ly.getChildCount(); i++) {
                LinearLayout l = (LinearLayout) ly.getChildAt(i);
                ViewGroup child_view = l;

                for (int j = 0; j < child_view.getChildCount(); j++) {
                    View childView = parentView.getChildAt(i);


                     if(childView instanceof TextView){

                        TextView t = (TextView) childView;
                Log.i("VVVVVVVVVVVVVVVVVVVVV", t.getText().toString());

                     }

                }

            }
        }
    };

}

在这里,我使用代码动态生成textviews,EditText和Button。它工作正常但我需要在单击按钮时获取textviews中的所有值,但它不起作用。任何人都可以帮我解决这个问题。< / p>

2 个答案:

答案 0 :(得分:1)

您已经两次获得parentView的孩子。

View childView = parentView.getChildAt(i);

应该是

View childview = child_view.getChildAt(j);

答案 1 :(得分:0)

试试这个

for (int j = 0; j < child_view.getChildCount(); j++) {
                    View childView = parentView.findViewById(R.id.yourTextViewId);

                        TextView t = (TextView) childView;
                Log.i("VVVVVVVVVVVVVVVVVVVVV", t.getText().toString());


                }