2015-06-15 21 views
1

我做了一個使用Volley的博客閱讀器。我在創建WebView時顯示文章(在文章中打開WebView),但是我在使用onListItemClick時遇到了問題。問題是:基於我學到的東西,我應該把JSONArray的「名稱」,但問題是,我沒有這個名字。它看起來像這樣:如何通過onListItemClick(使用Volley/JSON)打開WebView?

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     try { 
      JSONArray jsonPosts = obj.getJSONArray(**!!!I don't know what should I put here!!!**); 
      JSONObject jsonPost = jsonPosts.getJSONObject(position); 
      String blogUrl = jsonPost.getString(new String(obj.getString("url").getBytes("ISO-8859-1"), "UTF-8")); 

      Intent intent = new Intent(this, WebViewActivity.class); 
      intent.setData(Uri.parse(blogUrl)); 
      startActivity(intent); 
     } catch (JSONException e) { 
      Log.e(TAG, "Exception caught!", e); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
    } 

JSON代碼如下所示:

[ 
{ 
"id": 58, 
"url": "http://integrallab.ru/index.php/categorii-so-statyami/2013-10-25-13-26-29/spiralnaya-dinamika", 
"title": "some data", 
"time": "15 min", 
"author": "name of the author", 
"icon": "http://integrallab.hol.es/thumbnail/spiral_dynamics.jpg" 
}, 
{ 
.................................... 

MainActivity.java

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

// Posts json url 
private static final String url = "http://integrallab.hol.es/document9.json"; 
private ProgressDialog pDialog; 
private List<Post> postList = new ArrayList<Post>(); 
private ListView listView; 
private CustomListAdapter adapter; 
private JSONObject obj; 

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

    listView = (ListView) findViewById(android.R.id.list); 
    adapter = new CustomListAdapter(this, postList); 
    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 postReq = 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 { 

          obj = response.getJSONObject(i); 
          Post post = new Post(); 
          post.setTitle(new String(obj.getString("title").getBytes("ISO-8859-1"), "UTF-8")); 

          post.setThumbnailUrl(obj.getString("icon")); 
          post.setAuthor(new String(obj.getString("author").getBytes("ISO-8859-1"), "UTF-8")); 
          post.setTime(new String(obj.getString("time").getBytes("ISO-8859-1"), "UTF-8")); 

          // adding post to posts array 
          postList.add(post); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } catch (UnsupportedEncodingException 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(postReq); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    try { 
     JSONArray jsonPosts = obj.getJSONArray(); 
     JSONObject jsonPost = jsonPosts.getJSONObject(position); 
     String blogUrl = jsonPost.getString(new String(obj.getString("url").getBytes("ISO-8859-1"), "UTF-8")); 

     Intent intent = new Intent(this, WebViewActivity.class); 
     intent.setData(Uri.parse(blogUrl)); 
     startActivity(intent); 
    } catch (JSONException e) { 
     Log.e(TAG, "Exception caught!", e); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 
} 

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

}

回答

1

我相信你不必去通過從onListItemClick中獲取JSONArray和/或其中的物體的麻煩。

您可以簡單地將一個新字段url添加到您的Post類。然後裏面

public void onResponse(JSONArray response) 

你在哪裏解析您JSONResponse並把相應的數值,Post對象字段,解析url爲好。現在

post.setUrl(new String(obj.getString("url").getBytes("ISO-8859-1"), "UTF-8")); 

,您已經爲每個列表項Post對象,每個Post對象有url,你稱之爲blogUrl

至於你的項目點擊監聽器,下面應該足夠了您的需求

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Post p = (Post) l.getItemAtPosition(position); 
    String blogUrl = p.getUrl(); 
    Intent intent = new Intent(this, WebViewActivity.class); 
    intent.setData(Uri.parse(blogUrl)); 
    startActivity(intent); 
} 

希望這有助於!

+0

非常感謝!它真的幫助了我! – Nikolai

相關問題