如何通过onListItemClick(使用Volley/JSON)打开WebView?

How to open WebView through the onListItemClick (using Volley/JSON)?

我使用 Volley 创建了一个博客 reader。当您按下列表时,我创建了 WebView 来显示文章(打开带有文章的 WebView),但是我在使用 onListItemClick 时遇到了问题。问题是:根据我所学,我应该把 "name" of JSONArray,但问题是,我没有这个名字。它看起来像这样:

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

}

我相信您不必费心从 onListItemClick.

中获取 JSONArray and/or 对象

您只需将新字段 url 添加到您的 Post class。然后里面

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

希望对您有所帮助!!