Android Json数组对象填充textview

时间:2013-11-07 16:46:43

标签: java android json arraylist textview

我有一个http json数组对象到我的FeedItem Serializable类,我用它来填充listview。一切正常,但在另一个片段我有一个textview,我希望从我下载的json数组填充一个字符串我尝试了很多方法仍然无法做到这一点。

这是在json下载后开始的空白

public void updateList() {

    **TextView infoz = (TextView) getView().findViewById(R.id.infoz);**
    infoz.setText(getString(feedList))

    feedListView= (ListView)getActivity().findViewById(R.id.custom_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 Data = (FeedItem) o;


            Intent intent = new Intent(getActivity(), FeedDetailsActivity.class);
            intent.putExtra("feed", Data);
            startActivity(intent);
        }
    });
}

feedList是一个下载了json的数组。我应该如何使用字符串并在textview中设置它? 感谢

自定义列表适配器

    public class CustomListAdapter extends BaseAdapter {

    private ArrayList<FeedItem> listData;

    private LayoutInflater layoutInflater;

    private Context mContext;

    public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
            this.listData = listData;
            layoutInflater = LayoutInflater.from(context);
            mContext = context;
    }

    @Override
    public int getCount() {
            return listData.size();
    }

    @Override
    public Object getItem(int position) {
            return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
            return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                    convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
                    holder = new ViewHolder();
                    holder.headlineView = (TextView)convertView.findViewById(R.id.title);
                    holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
                    holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);

                    convertView.setTag(holder);
            } else {
                    holder = (ViewHolder) convertView.getTag();
            }

            FeedItem newsItem = (FeedItem) listData.get(position);
            holder.headlineView.setText(newsItem.getTitle());
            holder.reportedDateView.setText(newsItem.getDate());

            if (holder.imageView != null) {
                    new ImageDownloaderTask(holder.imageView).execute(newsItem.getAttachmentUrl());
            }

            return convertView;
    }

    static class ViewHolder {

            TextView headlineView;
            TextView reportedDateView;
            ImageView imageView;
    }




    }

json

 public void parseJson(JSONObject json) {
      try {

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




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


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

            feedList.add(item); 

              }

               } 

0 个答案:

没有答案