簡體   English   中英

Android json數組到另一個textview

[英]Android json array to another textview

我有3個片段頁面應用程序,在第一個和第三個片段中,我有listviews,在第二個片段中,只有textview(infoz)。 該代碼工作正常,但是我無法在第二個片段中將json字符串添加到textview中,我嘗試了幾種方法,但沒有成功。 TextView infoz =(TextView)getView()。findViewById(R.id.infoz); 這是我要用數組中的字符串填充的textview,我該怎么辦?

這是代碼

  public class Fragment2 extends Fragment {

 private ArrayList<FeedItem> feedList;
 private ArrayList<SecondFeedItem> secondfeedList;
 private ListView feedListView;
 private ListView secondfeedListView;





  @Override  
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
      View rootView = inflater.inflate(R.layout.home, container, false);


      String url = "";
      new DownloadFilesTask().execute(url);
    return rootView;  

  }


  public void updateList() {


      TextView infoz = (TextView) getView().findViewById(R.id.infoz);





      feedListView= (ListView)getActivity().findViewById(R.id.custom_list);
      secondfeedListView = (ListView)getActivity().findViewById(R.id.second_list);


      feedListView.setAdapter(new CustomListAdapter(getActivity(), feedList));
      feedListView.setOnItemClickListener(new OnItemClickListener() {



              @Override
              public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                      Object o = feedListView.getItemAtPosition(position);
                      FeedItem newsData = (FeedItem) o;


                      Intent intent = new Intent(getActivity(), FeedDetailsActivity.class);
                      intent.putExtra("feed", newsData);
                      startActivity(intent);
              }
      });
      secondfeedListView.setAdapter(new secondCustomListAdapter (getActivity(), secondfeedList));
      secondfeedListView.setOnItemClickListener(new OnItemClickListener() {



              @Override
              public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                      Object o = secondfeedListView.getItemAtPosition(position);
                      secondFeedItem secondData = (secondFeedItem) o;


                      Intent intent = new Intent(getActivity(), secondFeedDetailsActivity.class);
                      intent.putExtra("secondfeed", secondData);
                      startActivity(intent);
              }
      });
       }


         public class DownloadFilesTask extends AsyncTask<String, Integer, Void> {

      @Override
      protected void onProgressUpdate(Integer... values) {
      }

      @Override
      protected void onPostExecute(Void result) {
              if (null != feedList) {
                      updateList();
              }
              if (null != badfeedList) {
                  updateList();
          }
      }

      @Override
      protected Void doInBackground(String... params) {
              String url = params[0];

              // getting JSON string from URL
              JSONObject json = getJSONFromUrl(url);

              //parsing json data
              parseJson(json);
              return null;
      }
     }


       public JSONObject getJSONFromUrl(String url) {
      InputStream is = null;
      JSONObject jObj = null;
      String json = null;

      // Making HTTP request
      try {
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);

              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();

              BufferedReader reader = new BufferedReader(new InputStreamReader(
                              is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
              }
              is.close();
              json = sb.toString();
      } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
      } catch (ClientProtocolException e) {
              e.printStackTrace();
      } catch (IOException e) {
              e.printStackTrace();
      }

      try {
              jObj = new JSONObject(json);
      } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
      }

      // return JSON String
      return jObj;

         }

      public void parseJson(JSONObject json) {
      try {

              // parsing json object
              if (json.getString("status").equalsIgnoreCase("ok")) {
                      JSONArray posts = json.getJSONArray("posts");


                      String inffoz;

                      feedList = new ArrayList<FeedItem>();

                      for (int i = 0; i < posts.length(); i++) {
                              JSONObject post = (JSONObject) posts.getJSONObject(i);
                              FeedItem item = new FeedItem();

                              item.setTitle(post.getString("title"));
                              item.setDate(post.getString("description"));
                              item.setId(post.getString("id"));
                              item.setUrl(post.getString("url"));
                              item.setContent(post.getString("description"));
                              item.setsecondtitle(post.getString("description"));

                              JSONArray attachments = post.getJSONArray("attachments");
                              item.setsecondtitle(post.getString("secondtitle"));
                              if (null != attachments && attachments.length() > 0) {
                                      JSONObject attachment = attachments.getJSONObject(0);
                                      if (attachment != null)
                                              item.setAttachmentUrl(attachment.getString("url"));
                              }

                              feedList.add(item); 

                      }

                      } 






              if (json.getString("status").equalsIgnoreCase("ok")) {
                  JSONArray posts = json.getJSONArray("posts");

                      secondfeedList = new ArrayList<secondFeedItem>();

                      for (int i = 0; i < posts.length(); i++) {
                              JSONObject post = (JSONObject) posts.getJSONObject(i);
                              secondFeedItem item = new secondFeedItem();
                              item.setTitle(post.getString("title"));
                              item.setDate(post.getString("description"));
                              item.setId(post.getString("id"));
                              item.setUrl(post.getString("url"));
                              item.setContent(post.getString("description"));


                              JSONArray attachments = post.getJSONArray("attachments");

                              if (null != attachments && attachments.length() > 0) {
                                      JSONObject attachment = attachments.getJSONObject(0);
                                      if (attachment != null)
                                              item.setAttachmentUrl(attachment.getString("url"));
                              }

                              secondfeedList.add(item); 

                      }

              }




      } catch (JSONException e) {
              e.printStackTrace();
      }
     }




   }

json

 {
  "status": "ok",
  "count": 10,
  "posts": [
   {
      "id": 2398,
      "type": "post",
      "slug": "",
      "url": "",
      "status": "publish",
      "title": "bla",
      "description": "bla",
      "date": "2013-06-11 ",
      "content": "dadadadaddaadad",
      "secondbadtitle": "blaaaaaaa",
      "attachments": [
         {
            "id": 2342,
            "url": "",
            "slug": "",
            "title": "",
            "description": "",
            "content": "dadadadaddaadad", 
            "caption": "ssss",
            "parent": 2398,
            "mime_type": "image/jpg",
            "images": []
         }

     ],
     "comment_count": 3
  }
}

嘗試這個.......

 List list=new ArrayList();
        JSONObject jObject  = new JSONObject(str_response_starter);

        JSONArray json_array_item_id = jObject.getJSONArray("itemid");
        System.out.println("json array item id"+json_array_item_id);

        JSONArray json_array_item_name = jObject.getJSONArray("itemname");
        System.out.println("json array item name"+json_array_item_name);


        JSONArray json_array_item_type = jObject.getJSONArray("type");
        System.out.println("json array item type"+json_array_item_type);

        JSONArray json_array_item_cost = jObject.getJSONArray("cost");
        System.out.println("json array item cost"+json_array_item_cost);

如果您提供JSON格式,對我們會更好。 通過使用收集類,它是如此簡單。 如果您有任何問題,請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM