2014-01-30 31 views
0

我没有获取动态生成的TextView的值。无法获取动态生成的文本视图值

我的代码,

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 dynamically.It工作正常,但我需要在textviews所有值当我点击一个按钮,但它不工作。任何人都可以帮我解决这个问题。

回答

1

你得到的父视图的孩子两次。

View childView = parentView.getChildAt(i); 

应该

View childview = child_view.getChildAt(j); 
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()); 


       }