【问题标题】:Unable to show Toast from onItemClick()无法从 onItemClick() 显示 Toast
【发布时间】:2016-03-02 11:22:13
【问题描述】:

有人可以帮助我请告诉我如何在 Toast 中显示房间 ID 下面是代码。下面的代码工作,但我无法在 Toast 中显示 RoomId。

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

下面是哈希

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;
}

请帮助我如何在 TOast 中显示房间 ID

【问题讨论】:

  • 你的 onItemClick() 被调用了吗?只需保留一个调试点并检查
  • 您正在从 JSON 中获取值,但您从未使用方法 createRoom 创建 roomList。我确定您的列表没有显示任何内容。而且您不会在 for 循环中添加侦听器。它必须在 for 之外。
  • 每次调用findViewById(R.id.ChatlistView);,每次都在循环中设置Adapter,你到底在做什么?
  • 只是这段代码的bug太多了。
  • 现在你告诉我这个 Toast 出现了吗?

标签: android json android-toast


【解决方案1】:

替换

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

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

【讨论】:

  • 它有什么不同?
  • 它是否有效?我的意思是 toast 正在显示位置。?
  • 这没什么区别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多