1

伙计们,我正在尝试在下面解析这个 JSON

{"response": 
{"message": "Success", 
"code": 2002, 
"payload": 
    {"article": [
                    {"timestamp_epoch": 1359967680, 
                    "search_score": 10.0, 
                    "headline": "When Sachin felt like bunking cricket practice", 
                    "timestamp": "2013-02-04 08:48:00", 
                    "related_article_count": 0, 
                    "excerpt": "...their campaign 'Support my School' by NDTV in Mumbai on Sunday. Sachin Tendulkar and Aishwarya Rai Bacchan during an event 3/5 Sachin Tendulkar and Aishwarya Rai Bacchan during an event India's Sachin Tendulkar with actress Aishwarya Rai Bachchan during the...", 
                    "source": {"name": "OneIndia", 
                                "url": "http://www.oneindia.in/", 
                                "daylife_url": "http://www.daylife.com/source/OneIndia", 
                                "rank": 3, 
                                "favicon_url": "http://favicon.daylife.com/imageserve/0e0Jd1Xczb7oW/favicon.png", 
                                "source_id": "0e0Jd1Xczb7oW", 
                                "type": "MAINSTREAM"}, 
                    "url": "http://feedproxy.google.com/~r/oneindia-cricket/~3/z1pS6w289SU/when-sachintendulkar-felt-like-bunking-cricket-practice-066016.html", 
                    "daylife_url": "http://www.daylife.com/article/07nC5eTbBE0YA", 

                    "article_id": "07nC5eTbBE0YA"}, 


                    {"timestamp_epoch": 1360361400, 
                    "search_score": 8.69279902947, 
                    "headline": "Sachin equals Sunny",
                           .
                           .
                           .
                           .
                           .

我只对标题网址值感兴趣。我很容易用下面的这个java代码得到这些值。

JSONObject root = new JSONObject(result);
            JSONObject response = root.getJSONObject("response");

            String attributeId = response.getString("message");
            System.out.println(attributeId);

            JSONObject payload = response.getJSONObject("payload");
            JSONArray article = payload.getJSONArray("article");



            for (int i = 0; i < article.length(); i++) {
                JSONObject c = article.getJSONObject(i);
                String headline = c.getString("headline");
                String url = c.getString("url");
                System.out.println(url);
                System.out.println(headline);
                strings.add(c.getString("headline").toString());
            }
            arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_list_item_1, strings);

            resultList.setAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();

在这里,我在ListView中设置标题(这部分工作正常)。我的问题是:当我单击任何列表时,它应该将相应的url传递给WebView活动(实际上我正在另一个类中实现它)。那么如何实现这一点,如何存储对应的url和标题?请有人帮助我..

提前致谢..

4

2 回答 2

1

您可以创建一个名为 Article 的类,该类具有两个属性(字符串标题和字符串 url)。然后为这两个属性生成 getter/setter。现在创建一个 Article 类型的 ArrayList,例如..

ArrayList<Article> articleList = new ArrayList<Article>();

然后在解析你的数据时做..

Article article = new article();

并将价值放入对象中,例如..

article.setHeading(c.getString("headline"));
article.setUrl(c.getString("url"));

并将对象添加到arraylist,如..

articleList.add(article);

毕竟,当单击任何列表项时,要在您的主要活动中添加 OnItemClickListener 。它会给你点击项目的位置。然后你可以从 arcleList 中获取相应对象的 url,比如..

articleList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            String url = articleList.get(position).getUrl();
            Intent i = new Intent(MainActivity.this,   WebViewActivity.class);
            i.putExtra("url", url);
            startActivity(i);
        }
    });

现在将此 url 放入主要活动中的意图并将其接收到 webActivity 并最终在 webView 中使用它......

于 2013-02-11T15:22:00.387 回答
0

创建一个自定义适配器,它将 URL、标题作为模型存储并在 onView 中显示标题文本,并在 listview 的 onitemclicklistener 中访问适配器内的模型 obj 并使用该 URL 打开 webview ......就像这个http://www.ezzylearning。 com/tutorial.aspx?tid=1763429

和 itemclick 监听器

listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

  public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
  Model item = (Model)contests_listView.getItemAtPosition(position);  
  Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.getUrl()));
  startActivity(browserIntent);  // show url in browser  or pass it to your webview activity

     }
 });
于 2013-02-11T15:12:12.493 回答