Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

android - Unable to show Toast

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":".~&quot;LoVeRs_RoOM&quot;~."},{"roomid":"room30","room":".PunjaBi_RocK."},{"roomid":"room31","room":".~&quot;FrienDs_RoOM&quot;~."},{"roomid":"room33","room":".~&quot;Party_All_NiGhT&quot;~."},{"roomid":"room13","room":".~=HuNtEd-RooM=~."}]}

How to Show Room Id In Toast And OnItemClickListener.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...