2014-01-31 25 views
-1
public class Stock_check_fragment extends Fragment { 
    private CustomAutoCompleteTextView2 select_product; 
    ArrayAdapter<String> adapter; 

    LinearLayout ly; 
    int i = 0; 
    ArrayList<HashMap<String, String>> values = new ArrayList<HashMap<String, String>>(); 

    String[] from = { "brand", "weight" }; 

    // Ids of views in listview_layout 
    int[] to = { R.id.brand, R.id.weight }; 
    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); 

     select_product = (CustomAutoCompleteTextView2) rootView 
       .findViewById(R.id.select_product); 
     btn = (Button) rootView.findViewById(R.id.button1); 
     next = (Button) rootView.findViewById(R.id.next); 

     populate_produts(); 

     SimpleAdapter adapter = new SimpleAdapter(getActivity(), values, 
       R.layout.autocomplete2, from, to); 

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

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

     return rootView; 

    } 

    private OnItemClickListener autoItemSelectedListner = new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      HashMap<String, String> hm = (HashMap<String, String>) arg0 
        .getAdapter().getItem(arg2); 

      Add_text(select_product.getText().toString(), hm.get("weight") 
        .toString(), hm.get("Product_ID").toString()); 
     } 
    }; 

    public void Add_text(String value, String weight2, String Product_ID) { 

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

     TextView product = new TextView(getActivity()); 
     product.setText(value); 
     product.setPadding(10, 10, 10, 10); 
     product.setTag("product"); 
     ll.addView(product); 


     TextView weight = new TextView(getActivity()); 
     weight.setText(weight2); 
     weight.setPadding(10, 10, 10, 10); 
     product.setTag("weight"); 

     ll.addView(weight); 


     TextView product_ID = new TextView(getActivity()); 
     product_ID.setText(Product_ID); 
     product_ID.setPadding(10, 10, 10, 10); 
     product_ID.setTag("Product_ID"); 

     ll.addView(product_ID); 

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

     ll.addView(qty); 

     Button btn = new Button(getActivity()); 
     btn.setLayoutParams(params); 
     btn.setOnClickListener(o); 

     btn.setText("Add"); 
     btn.setTag("Add"); 
     btn.setWidth(100); 
     btn.setPadding(20, 20, 20, 20); 
     ll.addView(btn); 


     ly.addView(ll); 
     i++; 

    } 

    public void populate_produts() { 
     HashMap<String, String> temp = new HashMap<String, String>(); 
     temp.put("brand", "MONT Blanc"); 
     temp.put("weight", "200.00$"); 
     temp.put("Product_ID", "200.00$"); 
     values.add(temp); 


    } 

    OnClickListener o = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ViewGroup layout = (ViewGroup) v.getParent(); 
      if (null != layout) // for safety only as you are doing onClick 
       ly.removeView(layout); 

     } 
    }; 

    OnClickListener o1 = new OnClickListener() { 

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

      String qty = null; 
      String Product_ID = null; 

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

       ViewGroup child_view = l; 

       for (int j = 0; j < child_view.getChildCount(); j++) { 
        View childView = child_view.getChildAt(j); 
        **Log.i("sddddddddddddddddd", childView.getTag().toString());** 



       } 



      } 
     } 

    } 

我想上面的代碼創建3個文本視圖和一個編輯文本和動態一個鍵它的工作原理fine.But當我需要得到所有包含在動態小部件的標籤的標籤時,我點擊一個按鈕。但它給出了一個零點例外。無法獲得部件

01-31 15:45:11.710: E/AndroidRuntime(15061): java.lang.NullPointerException 
01-31 15:45:11.710: E/AndroidRuntime(15061): at ceylon.linux.osrumsalespad.Stock_check_fragment$3.onClick(Stock_check_fragment.java:197) 
+0

對不起我添加錯誤日誌now.Line 197 = ** Log.i( 「sddddddddddddddddd」,childView.getTag( ).toString()); ** – user2771059

回答

1
TextView weight = new TextView(getActivity()); 
     weight.setText(weight2); 
     weight.setPadding(10, 10, 10, 10); 
     product.setTag("weight"); 

我覺得這個產品應該是體重變化

TextView weight = new TextView(getActivity()); 
      weight.setText(weight2); 
      weight.setPadding(10, 10, 10, 10); 
      weight.setTag("weight"); 
+0

Thanx u救我的日子lahiru – user2771059