0
我試圖通過數據加載到我的列表視圖與volley.i知道如何通過圖像和文本通過活動,但不是JSON數據。它可能是一個重複的問題,但我沒有幫助其他答覆。 下面是我的活動代碼:傳遞json數據使用凌空下載活動
public class Movies extends ActionBarActivity{
\t // 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;
\t @Override
\t public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event);
\t
\t getSupportActionBar().setDisplayHomeAsUpEnabled(true);
\t Intent newActivity2=new Intent();
\t setResult(RESULT_OK, newActivity2);
\t 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) {
\t if (error instanceof NoConnectionError){
\t 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(){
\t @Override
\t public void onItemClick(AdapterView<?> parent, View view,
\t int position, long id) {
\t \t
\t \t Intent intent = new Intent(Movies.this, Detail.class);
\t startActivity(intent);
\t }
\t });}
@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;
}
}
我應該實現通過帕拉姆時,此代碼段:
intent.putExtra("json",jsonobj.toString());
和我的第二個活動;
JSONObject obj=new JSONObject(getIntent().getStringExtra("json")
但我不得到怎樣處理我自己的活動code.please幫助替換 「JSON」,jsonobj.toString()。
在此先感謝
你有兩個選擇:1)傳遞json原樣(即:作爲一個字符串)並在接收端(第二個活動)再次解析它。 2)製作你的'Movie'類'Parcelable'(或'Serializable'),並將數據作爲Java對象傳遞給第二個活動。順便說一下,在你的情況下,相當於'jsonobj.toString()'是'obj.toString()'。 –
@ MH.i'd與第一個選項,我得到膨脹的JSON,但我得到'obj'的錯誤標記,它不能解決。問題是什麼?有一些代碼丟失? –
你在'onResponse()'方法中得到了json字符串,所以如果你想掛在它上面,那你必須把它分配給一個成員變量,然後你可以訪問它(當你創建'Intent')。 –