Domanda

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());**



                }



            }
        }

    }

I tried above code to create 3 text views and one edit text and one button dynamically it works fine.But when i need to get the all the tags that included in dynamic widgets when i click a button .But it gives a null point exception.

 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)
È stato utile?

Soluzione

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

i think this product should change as weight

  TextView weight = new TextView(getActivity());
            weight.setText(weight2);
            weight.setPadding(10, 10, 10, 10);
            weight.setTag("weight");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top