Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 实现MClickListener,然后在另一个活动中显示JSON数据_Android_Json_Listview - Fatal编程技术网

Android 实现MClickListener,然后在另一个活动中显示JSON数据

Android 实现MClickListener,然后在另一个活动中显示JSON数据,android,json,listview,Android,Json,Listview,如何在ListView中加载特定的JSON数据,该数据包含单击的ListView项中包含的所有数据?我不知道如何在detailinfo中显示json数据 主要活动 public class MainActivity extends Activity { // Log tag private static final String TAG = MainActivity.class.getSimpleName(); // Movies json url private static final

如何在ListView中加载特定的JSON数据,该数据包含单击的ListView项中包含的所有数据?我不知道如何在detailinfo中显示json数据

主要活动

public class MainActivity extends Activity {

// Log tag
private static final String TAG = MainActivity.class.getSimpleName();

// Movies json url
private static final String url = "http://api.androidhive.info/json/movies.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, movieList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            if (position == 0) {
                Intent myIntent = new Intent(view.getContext(), detailinfo.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 1) {
                Intent myIntent = new Intent(view.getContext(), detailinfo2.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 2) {
                Intent myIntent = new Intent(view.getContext(), detailinfo3.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 3) {
                Intent myIntent = new Intent(view.getContext(), detailinfo.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 4) {
                Intent myIntent = new Intent(view.getContext(), detailinfo2.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 5) {
                Intent myIntent = new Intent(view.getContext(), detailinfo3.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 6) {
                Intent myIntent = new Intent(view.getContext(), detailinfo.class);
                startActivityForResult(myIntent, 0);
            }

            if (position == 7) {
                Intent myIntent = new Intent(view.getContext(), detailinfo2.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();


    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("title"));
                            movie.setThumbnailUrl(obj.getString("image"));
                            movie.setRating(((Number) obj.get("rating"))
                                    .doubleValue());
                            movie.setYear(obj.getInt("releaseYear"));

                            // Genre is json array
                            JSONArray genreArry = obj.getJSONArray("genre");
                            ArrayList<String> genre = new ArrayList<String>();
                            for (int j = 0; j < genreArry.length(); j++) {
                                genre.add((String) genreArry.get(j));
                            }
                            movie.setGenre(genre);

                            // adding movie to movies array
                            movieList.add(movie);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();

        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);
}

@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}
电影

public class Movie {
private String title, thumbnailUrl;
private int year;
private double rating;
private ArrayList<String> genre;

public Movie() {
}

public Movie(String name, String thumbnailUrl, int year, double rating,
             ArrayList<String> genre) {
    this.title = name;
    this.thumbnailUrl = thumbnailUrl;
    this.year = year;
    this.rating = rating;
    this.genre = genre;
}

public String getTitle() {
    return title;
}

public void setTitle(String name) {
    this.title = name;
}

public String getThumbnailUrl() {
    return thumbnailUrl;
}

public void setThumbnailUrl(String thumbnailUrl) {
    this.thumbnailUrl = thumbnailUrl;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public double getRating() {
    return rating;
}

public void setRating(double rating) {
    this.rating = rating;
}

public ArrayList<String> getGenre() {
    return genre;
}

public void setGenre(ArrayList<String> genre) {
    this.genre = genre;
}

}
公共类电影{
私有字符串标题,缩略图URL;
私人国际年;
私人双重评级;
私人阿雷亚主义流派;
公共电影(){
}
公共电影(字符串名称、字符串缩略图URL、整数年、双评级、,
ArrayList流派){
this.title=名称;
this.thumbnailUrl=thumbnailUrl;
今年=年;
这个。评级=评级;
这个。流派=流派;
}
公共字符串getTitle(){
返回标题;
}
public void setTitle(字符串名称){
this.title=名称;
}
公共字符串getThumbnailUrl(){
返回缩略图URL;
}
公共void setThumbnailUrl(字符串thumbnailUrl){
this.thumbnailUrl=thumbnailUrl;
}
公共int getYear(){
回归年;
}
公共年(国际年){
今年=年;
}
公共双重评级(){
回报率;
}
公共评级(双重评级){
这个。评级=评级;
}
公共ArrayList getgreen(){
回归体裁;
}
公共无效集合类型(ArrayList类型){
这个。流派=流派;
}
}
它携带单击的ListView项中包含的所有数据

一般来说,使用意图附加项


更快的方法是在movie类上实现Parcelable接口

您可以通过在下一个活动中使用的意图传递可包裹对象


要获取单击的项目,您可以使用适配器。getItem(position)非常好的答案如下:

您只需要使您的电影类实现可序列化。 然后在onClick pass movie对象中,就像intent.putExtra(“movie”,movie)

在第二个活动中,将其作为
getIntent.getSerializableExtra(“电影”)
使用

将Java对象转换为JSON

String jsonInString = gson.toJson(obj);
将JSON转换为Java对象

YourClass yourClass = gson.fromJson(jsonInString, YourClass .class);

将Java对象转换为itemClick上的JsonString并通过intent将其发送给detailActivity,然后在detailActivity中获取intent数据并将其转换为Java对象。

另一种解决方案是将对象序列化为json字符串,然后在第二个activity中反序列化。是的,这是可能的。谢谢,但是如果我只想传递特定的json数据(标题、图像和评级),怎么办?为什么要在json中传递这些数据?我的意思是希望标题、图片、收视率都是字符串,你们可以在意图中单独传递。截击是为了提出一个高效的服务器请求。它不用于在活动之间通信数据。您的意思是使用详细信息活动中的截取来发出新的服务器请求吗?谢谢,伙计,但在这种情况下,我使用截取来解析json,我可以简单地调用详细信息活动中的截取来显示json数据吗?意图附加的大小有限,因此json并不总是最好的@默认情况下,Moacito-Volley不会将JSON解析为字符串或JSONObject以外的任何内容。这些库做不同的事情,可以一起使用。
YourClass yourClass = gson.fromJson(jsonInString, YourClass .class);