问题描述
的有些人可以帮助我,请叫我怎么展示室ID在吐司下面是code。低于code工作,但我不能够以显示吐司RoomId。的
MainActivity.java
包com.android.listviewexample;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;
进口的java.util.Map;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;
进口com.android.volley.AuthFailureError;
进口com.android.volley.Request;
进口com.android.volley.RequestQueue;
进口com.android.volley.Response;
进口com.android.volley.VolleyError;
进口com.android.volley.toolbox.StringRequest;
进口com.android.volley.toolbox.Volley;
进口android.support.v7.app.ActionBarActivity;
进口android.view.View;
进口android.view.Window;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.ListView;
进口android.widget.AdapterView;
进口android.widget.SimpleAdapter;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;
公共类MainActivity扩展ActionBarActivity {私人请求队列请求队列;
私有静态最终字符串URL =http://192.168.0.1/api/enter.php;
私人StringRequest请求;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);请求队列= Volley.newRequestQueue(本);
请求=新StringRequest(Request.Method.POST,网址,新Response.Listener<串GT;(){
@覆盖
公共无效onResponse(字符串响应){
尝试{
的JSONObject的JSONObject =新的JSONObject(响应);
如果(jsonObject.names()得到(0).equals(成功)){JSONArray成功= jsonObject.getJSONArray(成功);
的for(int i = 0; I< success.length();我++){
JSONObject的更迭= success.getJSONObject(I)
的System.out.println(succes.toString());
}
JSONArray房= jsonObject.getJSONArray(房间);
的for(int i = 0; I< rooms.length();我++){
的JSONObject室= rooms.getJSONObject(ⅰ);
字符串名称= room.optString(房间);
串roomid = room.optString(roomid);
字符串输出=名称;
roomList.add(createRoom(聊天室,输出));ListView控件的ListView =(ListView控件)findViewById(R.id.examplelistView);
SimpleAdapter simpleAdapter =新SimpleAdapter(MainActivity.this,roomList,android.R.layout.simple_list_item_1,新的String [] {聊天室},新的INT [] {android.R.id.text1});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){意向意图=新意图(getApplicationContext(),FstActivity.class);
束B =新包();
INT itemPosition =位置;
b.putString(roomposition,位置:+ itemPosition);
//b.putString(\"roomid,roomid.toString());
intent.putExtras(二);
startActivity(意向);
Toast.makeText(getApplicationContext(),位置:+ itemPosition,Toast.LENGTH_LONG)
。显示();}
});
}}其他{}
}赶上(JSONException E){
e.printStackTrace();
}
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
}
}){
@覆盖
保护地图<字符串,字符串> getParams()方法抛出AuthFailureError {
HashMap的<字符串,字符串> HashMap的=新的HashMap<字符串,字符串>();
hashMap.put(S,e0c93d9bb49efd89f9b70225bd65f157的ToString());
返回的HashMap;
}
};
requestQueue.add(请求);}
清单<地图<字符串,字符串>> roomList =新的ArrayList<地图<字符串,字符串>>();
私人的HashMap<字符串,字符串> createRoom(字符串名称,串号){
HashMap的<字符串,字符串> ROOMNAME =新的HashMap<字符串,字符串>();
roomName.put(姓名,号码);
返回ROOMNAME;}
}
activity_main.xml中
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=com.android.listviewexample.MainActivity> < ListView控件
机器人:ID =@ + ID / examplelistView
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentRight =真正的> < /&的ListView GT;< / RelativeLayout的>
192.168.0.1/enter.php
<$p$p><$c$c>{\"success\":[{\"sid\":\"e0c93d9bb49efd89f9b70225bd65f157\",\"login\":\"saeed\"}],\"rooms\":[{\"roomid\":\"room12\",\"room\":\".~=cAsiNo-RooM=~.\"},{\"roomid\":\"room24\",\"room\":\".~=MusiC-Masti=~.\"},{\"roomid\":\"room29\",\"room\":\".~"LoVeRs_RoOM"~.\"},{\"roomid\":\"room30\",\"room\":\".PunjaBi_RocK.\"},{\"roomid\":\"room31\",\"room\":\".~"FrienDs_RoOM"~.\"},{\"roomid\":\"room33\",\"room\":\".~"Party_All_NiGhT"~.\"},{\"roomid\":\"room13\",\"room\":\".~=HuNtEd-RooM=~.\"}]}如何显示房间ID在烤面包和OnItemClickListener。
使用列表首先添加这两个项目如下code:
list.add(型号(名称,roomid));
另外创建一个模型类这样的RoomModel:
公共类RoomModel {
公共字符串名称,编号,图片,描述; 公共字符串的getName(){
返回名称;
} 公共无效setname可以(字符串名称){
this.name =名称;
} 公共字符串的getId(){
返回ID;
} 公共无效SETID(字符串ID){
this.id = ID;
}
}
和创建这样在你的活动类模型:
私人RoomModel模型(ID字符串,字符串名称){
RoomModel M =新RoomModel();
m.setId(ID);
m.setName(名);
返回米;
}
现在在OnItemClickListener电话:
Toast.makeText(getApplicationContext(),roomlist.get(位置).getId());, Toast.LENGTH_LONG)
。显示();
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.
MainActivity.java
package com.android.listviewexample;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.Window;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends ActionBarActivity {
private RequestQueue requestQueue;
private static final String URL = "http://192.168.0.1/api/enter.php";
private StringRequest request;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQueue = Volley.newRequestQueue(this);
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.names().get(0).equals("success")){
JSONArray success = jsonObject.getJSONArray("success");
for (int i = 0; i < success.length(); i++) {
JSONObject succes = success.getJSONObject(i);
System.out.println(succes.toString());
}
JSONArray rooms = jsonObject.getJSONArray("rooms");
for (int i = 0; i < rooms.length(); i++) {
JSONObject room = rooms.getJSONObject(i);
String name = room.optString("room");
String roomid = room.optString("roomid");
String outPut = name;
roomList.add(createRoom("chatroom", outPut));
ListView listView = (ListView) findViewById(R.id.examplelistView);
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.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) {
Intent intent = new Intent(getApplicationContext(), FstActivity.class);
Bundle b = new Bundle();
int itemPosition = position;
b.putString("roomposition", "Position :"+itemPosition);
//b.putString("roomid", roomid.toString());
intent.putExtras(b);
startActivity(intent);
Toast.makeText(getApplicationContext(),"Position :"+itemPosition, Toast.LENGTH_LONG)
.show();
}
});
}
}else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> hashMap = new HashMap<String, String>();
hashMap.put("s","e0c93d9bb49efd89f9b70225bd65f157".toString());
return hashMap;
}
};
requestQueue.add(request);
}
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;}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.android.listviewexample.MainActivity" >
<ListView
android:id="@+id/examplelistView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
</ListView>
</RelativeLayout>
192.168.0.1/enter.php
{"success":[{"sid":"e0c93d9bb49efd89f9b70225bd65f157","login":"saeed"}],"rooms":[{"roomid":"room12","room":".~=cAsiNo-RooM=~."},{"roomid":"room24","room":".~=MusiC-Masti=~."},{"roomid":"room29","room":".~"LoVeRs_RoOM"~."},{"roomid":"room30","room":".PunjaBi_RocK."},{"roomid":"room31","room":".~"FrienDs_RoOM"~."},{"roomid":"room33","room":".~"Party_All_NiGhT"~."},{"roomid":"room13","room":".~=HuNtEd-RooM=~."}]}
How to Show Room Id In Toast And OnItemClickListener.
first of all add both items in list using following code:
list.add(model(name, roomid));
Also create a model class for RoomModel like this:
public class RoomModel {
public String name, id, image, description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
and create a model in your activity class like this:
private RoomModel model(String id, String name) {
RoomModel m= new RoomModel();
m.setId(id);
m.setName(name);
return m;
}
Now in OnItemClickListener call:
Toast.makeText(getApplicationContext(),roomlist.get(position).getId());, Toast.LENGTH_LONG)
.show();
这篇关于JSON来的ListView随着setOnItemClickListener在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!