2017-06-08 32 views
-2

我试图从我的列表视图中检索项目名称和数量, 并将其显示到新类:Details.java。如何从Android上点击listview项获取值? (NullException error)

这里是我的代码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 
+0

或使用listView.getItemAtPosition(位置) –

回答

1

点击,选择的是不同的东西。

更换

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

SalesItemInformationLV saleitem = displayiteminfo.get(position) 
+0

哦,我见!选择的用途是什么?感谢您的帮助btw! :) – Shells

+0

当你需要选择的项目,你必须声明它可选等等。不记得确切的细节,多年没有使用它:) –

+0

好吧谢谢! :) – Shells

相关问题