单击列表视图时如何解析项目对象的其他活动?

时间:2018-12-19 20:09:48

标签: android json

这是我的JSON:

 {
  "NEWS": [
    {
      "ID": 1,
      "Title": "A",
      "News": "A"
    },
    {
      "ID": 2,
      "Title": "B",
      "News": "B"
    }
  ]
}

我正在列表视图中解析此JSON,并仅显示标题。现在,如果单击列表视图项,例如,单击“ A”,我想在其他活动中填充标题和新闻。我如何约束他们?

 ArrayList<HashMap<String, String>> newList ;

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

    newList = new ArrayList<>();
    String items = String.valueOf(newList);

    lv = (ListView) findViewById(R.id.list);

    new GetContacts().execute();
}

private class GetContacts extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                JSONArray NEWS= jsonObj.getJSONArray("NEWS");

                for (int i = 0; i < NEWS.length(); i++) {

                    JSONObject c = icerik.getJSONObject(i);
                    String ID= c.getString("ID");
                    String Title= c.getString("Title");
                    String News= c.getString("News");

                    HashMap<String, String> newss= new HashMap<>();

                    newss.put("ID", ID);
                    newss.put("Title", Title);
                    newss.put("News", News);

                    newList.add(newss);
                }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();

        final ListAdapter adapter = new SimpleAdapter(
                news.this, newList,
                R.layout.new_list, new String[]{"Title"},
                new int[]{
                        R.id.Ttile});

        lv.setAdapter(adapter);
        lv.setClickable(true);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position >= 0)
                {
                    //Intent myIntent = new Intent(view.getContext(), MYSECONDACTIVTY.class);
                    //startActivityForResult(myIntent, 0);
                }

我找到了it,并尝试了一下,但是没有用。我只想在单击列表项时显示其他活动的项详细信息。

1 个答案:

答案 0 :(得分:0)

将数据插入Bundle,将Bundle插入Intent

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent myIntent = new Intent(view.getContext(), MYSECONDACTIVTY.class);
        Bundle myBundle = new Bundle();
        myBundle.putString("title", newList.get(postiyion).get("Title");
        myBundle.putString("id", newList.get(postiyion).get("ID");
        myBundle.putString("news", newList.get(postiyion).get("News");
        myIntent.putExtras(mByundle);
        startActivityForResult(myIntent, 0);
}

MYSECONDACTIVTY中恢复这些值,例如onCreate内或您需要的任何地方

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    String title = getIntent().getExtras().getString("Title");
    String id = getIntent().getExtras().getString("ID");
    String news = getIntent().getExtras().getString("News")
}

这必须工作!还check this SO question以获取有关Bundle并传递给另一个Activity

的更多详细信息