Unable to show Toast from onItemClick()

2019-09-11 12:25发布

问题:

Some One Can Help Me Please Told Me How To Show Room Id In Toast Below Is The Code. Below Code Working But I am Not Able To Show RoomId in Toast.

JSONArray rooms = jsonObject.getJSONArray("rooms");
for (int i = 0; i < rooms.length(); i++) {
    JSONObject room = rooms.getJSONObject(i);
    System.out.println("List Room Query".toString());
    System.out.println(room.toString());
    String rname = room.optString("room");
    String rnameid = room.optString("roomid");
    String outPut2 = rnameid;
    ListView listView = (ListView) findViewById(R.id.ChatlistView);
    SimpleAdapter simpleAdapter = new SimpleAdapter(RoomListActivity.this, roomList, android.R.layout.simple_list_item_1, new String[] {"chatroom"}, new int[] {android.R.id.text1});
    listView.setAdapter(simpleAdapter);


    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            int itemPosition     = position;
            Toast.makeText(getApplicationContext(), "Position :"+itemPosition, Toast.LENGTH_LONG).show();
        }
    }); 
}    

Below is Hash

List<Map<String,String>> roomList = new ArrayList<Map<String,String>>();
private HashMap<String, String>createRoom(String name,String number){
    HashMap<String, String> roomName = new HashMap<String, String>(); 
    roomName.put(name, number); 
    return roomName;
}

Please Please Help Me How to Show Room Id In TOast

回答1:

Replace

Toast.makeText(getApplicationContext(),
        "Position :"+itemPosition, Toast.LENGTH_LONG)
        .show();

with

Toast.makeText(RoomListActivity.this,
        "Position :"+itemPosition, Toast.LENGTH_LONG)
        .show();