如何从Android上点击列表视图项获取值? (NullException错误)

时间:2017-06-08 15:27:36

标签: java android listview

我试图从列表视图中检索项目名称和数量, 并将其显示在新类:Details.java中。

以下是listview.java的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);

        //create items to display in customized listview (Arraylist)
        displayiteminfo.add(new SalesItemInformationLV("Bread", 2));
        displayiteminfo.add(new SalesItemInformationLV("Butter", 9));
        displayiteminfo.add(new SalesItemInformationLV("Margarine", 8));


        //New array adapter for customised ArrayAdapter
        final ArrayAdapter<SalesItemInformationLV> adapter = new itemArrayAdapter(this, 0, displayiteminfo);
        //displayiteminfo - the ArrayList of item objects to display.

        //Find the list view, bind it with custom adapter
        final ListView listView = (ListView)findViewById(R.id.customListview);
        listView.setAdapter(adapter);

        //Selecting the listview item!
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SalesItemInformationLV saleitem = (SalesItemInformationLV) listView.getSelectedItem();
 String namevalue = saleitem.getItemname(); ---> WHERE ERROR OCCURS
 int qtyvalue = saleitem.getItemquantity();

                Intent myintent = new Intent(ListView.this, Details.class);
                myintent.putExtra("itemname", namevalue);
                myintent.putExtra("itemqty", qtyvalue);

                startActivity(myintent);

            }
        });



    }





    //custom Arrayadapter
    class itemArrayAdapter extends ArrayAdapter<SalesItemInformationLV>
    {
        private  Context context;
        private List<SalesItemInformationLV> item;



        //constructor, call on creation
        public itemArrayAdapter(Context context, int resource, ArrayList<SalesItemInformationLV> objects) {

            //chaining to "default constructor" of ArrayAdapter manually
            super(context, resource, objects);
            this.context = context;
            this.item = objects;

        }

        //called to render the list

        public View getView(int position, View convertView, ViewGroup parent)
        {
            //get the item we are displaying
            SalesItemInformationLV iteminfo = item.get(position);

            //get the inflater and inflate the xml layout for each item
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.item_layout, null);

            //Each component of the custom item_layout
            TextView name = (TextView) view.findViewById(R.id.ItemNameSales);
            TextView qty = (TextView)view.findViewById(R.id.ItemNameQty);

            //set the name of item - access using an object!
            name.setText(String.valueOf(iteminfo.getItemname()));

            //set the quantity of item - access using an object!
            qty.setText(String.valueOf(iteminfo.getItemquantity()));

            return view;
            //Now return to onCreate to use this cuztomized ArrayAdapter
        }
    }

实施上述代码后,我收到了一个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ... on a null object reference

1 个答案:

答案 0 :(得分:1)

Clicked and Selected是不同的东西。

替换

SalesItemInformationLV saleitem = (SalesItemInformationLV) listView.getSelectedItem();

SalesItemInformationLV saleitem = displayiteminfo.get(position)