Java Android json数组到另一个textview

Java Android json数组到另一个textview,java,android,json,android-listview,Java,Android,Json,Android Listview,我有3个片段页面应用程序,在第一个和第三个片段中我有列表视图,在第二个片段中只有一个文本视图(infoz)。代码运行良好,但我无法在第二个片段中将json字符串添加到textview中。我尝试了几种方法,但没有成功。 TextView infoz=(TextView)getView().findviewbyd(R.id.infoz); 这是一个文本视图,我想用数组中的字符串填充它,我该怎么做 这是密码 public class Fragment2 extends Fragment { p

我有3个片段页面应用程序,在第一个和第三个片段中我有列表视图,在第二个片段中只有一个文本视图(infoz)。代码运行良好,但我无法在第二个片段中将json字符串添加到textview中。我尝试了几种方法,但没有成功。 TextView infoz=(TextView)getView().findviewbyd(R.id.infoz); 这是一个文本视图,我想用数组中的字符串填充它,我该怎么做

这是密码

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




   }

试试这个

 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的格式,对我们来说会更好。通过使用collection类,这是一项非常简单的任务。如果您遇到任何问题,请告诉我

您是否能够使用任何字符串设置textview的文本?是的,我使用infoz.settext(字符串)进行了测试;字符串=DSD;好吧,我只是想确认一下。您是否将JSON数组反编译为字符串?编辑:成功*所有json对象都转到feedList=new ArrayList();FeedItem类实现了Serializable你能告诉我你的JSON格式吗?我在问题中添加了JSON:)首先是JSONArray。然后首先将json字符串存储在JSONArray中,现在您可以通过get JSONArray使用count、status、comment\u count。现在Post是JSONObject,通过getJSONArray访问Post id的数据。如果有任何问题,请告诉我,但是如何将其添加到textview?setText(json数组项id);不要从那里开始工作,将其解析为字符串,或者尝试.setText(“+json\u array\u item\u id”);好的。你在哪里使用。setText?试试塞缪尔的建议。如果它起作用,那就投票吧。如果不行,就再打我一次
 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);