单击listView时不显示任何消息

时间:2016-01-03 08:57:33

标签: android mysql android-intent arraylist android-listview

Staff_Benefit 中,有一个listView,其中从MySQL检索数据和图片。单击列表时,它应显示iD

   List<Staff> staffs = new ArrayList();
   ListView listViewEdit;
   ArrayList<HashMap<String, Object>> StaffDetails;


      listViewEdit = (ListView)edit_details. findViewById(R.id.listViewEditStaff);
      StaffDetails = new ArrayList<HashMap<String, Object>>(); // error


         protected void showList(){  // retrieved value from MySQL
                try {
                    JSONObject jsonObj = new JSONObject(myJSON);
                    details= jsonObj.getJSONArray(Config.TAG_RESULTS);

                    for(int i=0;i<details.length();i++){
                        JSONObject c = details.getJSONObject(i);
                        String type = c.getString(Config.TAG_TYPE);
                        String description = c.getString(Config.TAG_DESCRIPTION);
                        String amount=c.getString(Config.TAG_AMOUNT);
                        String image=c.getString(Config.TAG_IMAGE);
                        int ID=c.getInt(Config.TAG_ID);
                       Staff staff= new Staff(ID,type,description,amount,image) ;
                        staffs.add(staff);
                    }

                   CVAdapter adapter = new CVAdapter(getActivity(),staffs);
                    listViewEdit.setAdapter(adapter); // retrieved data get displayed.

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

现在我将onItemClickListener实施为listView

     listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> listView, View view,
                                        int position, long id) {
                    mClickedPosition=position; // update
                    HashMap<String,Object> clickedItem = StaffDetails.get(position);
                    iD=clickedItem.get(Config.TAG_ID);
                    Toast.makeText(getActivity(),iD+"",Toast.LENGTH_LONG).show();
                   // Log.e("A", "A");
                }
            });

应用程序没有崩溃,但有一些东西显示在stacktrace

01-03 16:54:35.627  12678-12678/com.example.project.myapplication W/System.err﹕ java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-03 16:54:35.627  12678-12678/com.example.project.myapplication W/System.err﹕ at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
01-03 16:54:35.627  12678-12678/com.example.project.myapplication W/System.err﹕ at java.util.ArrayList.get(ArrayList.java:308)
01-03 16:54:35.627  12678-12678/com.example.project.myapplication W/System.err﹕ at com.example.project.myapplication.GUI.Edit_Staff_Benefit_ListView$1.onItemClick(Edit_Staff_Benefit_ListView.java:105)
01-03 16:54:35.627  12678-12678/com.example.project.myapplication W/System.err﹕ at android.widget.AdapterView.performItemClick(AdapterView.java:308) 

2 个答案:

答案 0 :(得分:0)

您不需要使用HashMap。我认为这就是弄乱你的东西。而是使用

 ArrayList<Staff> StaffDetails = new ArrayList<>();

并且在听众中使用staffs代替StaffDetailsStaffDetails中没有数据。 :3

希望这会有所帮助:)

答案 1 :(得分:0)

请尝试以下

istViewEdit.setOnItemClickListener(new OnItemClickListener() {