通过活动之间的凌空传递json数据

时间:2014-12-20 13:48:46

标签: android android-intent android-volley

我正在尝试将数据加载到我的listview中,使用volley.i知道如何通过活动而不是json数据传递图像和文本。这可能是一个重复的问题,但我没有帮助其他响应。 以下是我的活动代码:



public class Movies extends ActionBarActivity{
	// 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
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event);
	
   	 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

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

    // changing action bar color
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#1b1b1b")));

    // 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) {
                	if (error instanceof NoConnectionError){
                   	Toast.makeText(getBaseContext(), "Bummer..There's No Internet connection!", Toast.LENGTH_LONG).show();

                }};
            });

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

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

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 

    	@Override
    	public void onItemClick(AdapterView<?> parent, View view,
    	                        int position, long id) { 
    		
    		Intent intent = new Intent(Movies.this, Detail.class);     
    	    startActivity(intent);
    	}
    	});}

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



    }
 
&#13;
&#13;
&#13;

我应该在传递param时实现这段代码:

&#13;
&#13;
intent.putExtra("json",jsonobj.toString());
&#13;
&#13;
&#13;

以及我的第二项活动;

&#13;
&#13;
JSONObject obj=new JSONObject(getIntent().getStringExtra("json")
&#13;
&#13;
&#13;

但是我没有从我自己的活动代码中获取替换&#34; json&#34;,jsonobj.toString()。请帮助。

提前致谢

1 个答案:

答案 0 :(得分:0)

我能够弄明白并通过了标题。图像仍然在进行中。我正在这样做以帮助像我一样被卡住的人。这将给你提供线索。 在onCreate()之前插入:

	private static String Title="title";

然后在onClick()

String name = ((TextView) view.findViewById(R.id.title))
                .getText().toString();

        Intent intent = new Intent(Movies.this, Detail.class);    
        intent.putExtra(Title, name);
        startActivity(intent);

在第二个活动中,检索它:

Intent i=getIntent();
String name = i.getStringExtra(Title);

TextView lblName = (TextView) findViewById(R.id.name_label);

lblName.setText(name);}

在onCreate()之前放置以下内容后:

    private static String Title="title";

谢谢大家的帮助。