How do I show roomId In Toast
?
In my code (MainActivity) below, I am unable to show a Toast:
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.
See Question&Answers more detail:
os