2014-12-20 30 views
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()。

在此先感谢

+0

你有两个选择:1)传递json原样(即:作为一个字符串)并在接收端(第二个活动)再次解析它。 2)制作你的'Movie'类'Parcelable'(或'Serializable'),并将数据作为Java对象传递给第二个活动。顺便说一下,在你的情况下,相当于'jsonobj.toString()'是'obj.toString()'。 –

+0

@ MH.i'd与第一个选项,我得到膨胀的JSON,但我得到'obj'的错误标记,它不能解决。问题是什么?有一些代码丢失? –

+0

你在'onResponse()'方法中得到了json字符串,所以如果你想挂在它上面,那你必须把它分配给一个成员变量,然后你可以访问它(当你创建'Intent')。 –

回答

0

我能弄明白并通过了title.The图像仍然underway.I'm强制这样做是为了帮助别人谁被卡住了一样me.This会强制给你线索。然后在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); 

\t private static String Title="title";

而在第二活动,检索它一个这样的: 的onCreate()之前插入此

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"; 

感谢球员的所有帮助。