2014-01-30 66 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()); 


       }