0

I have http json array object to my FeedItem Serializable class, and i use it to populate listview. Everything works fine, but on another fragment i have a textview which i want to be populate with one string from my downloaded json array i and tried many ways and still can't do it.

this is the void who is starting after json download

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 is an array with downloaded json. How should i use on of the strings and set it in textview? Thanks

Custom List Adapter

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

              }

               } 
  • If FeedItem implements Serializable, you can pass that object as an extra. If you want to pass extra's to a Fragment, you have to pas Arguments to that Fragments similar like passing extra's in and Intent to an Activity. – DroidBender Nov 07 '13 at 16:54
  • @MartijnVanMierloo Can you please give me a example? – Sandra Mladenovic Nov 07 '13 at 17:02
  • I assume that your objects are correctly filled with the Json data. If you want to swap to another Fragment, you can pass data like described here: http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – DroidBender Nov 07 '13 at 17:05
  • private Intent getIntent(FeedItem intent) { intent.putExtra("feed", FeedItem.class); return null; } public void updateList() { FeedItem intent = new FeedItem(); TextView infoz = (TextView) getView().findViewById(R.id.infoz); FeedItem fi = (FeedItem)getIntent(intent).getExtras().get("badtitle"); infoz.setText(fi.getBadtitle()); – Sandra Mladenovic Nov 07 '13 at 17:30
  • I coded this, but woun't work, can you look up why? – Sandra Mladenovic Nov 07 '13 at 17:31

0 Answers0