Skip to content
This repository has been archived by the owner on Jul 22, 2019. It is now read-only.

Not working with object list #63

Open
Durgaprasad1541996 opened this issue May 7, 2018 · 0 comments
Open

Not working with object list #63

Durgaprasad1541996 opened this issue May 7, 2018 · 0 comments

Comments

@Durgaprasad1541996
Copy link

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener() {

                @Override
                public void onResponse(JSONObject response) {
                    try {

                        JSONArray jsonArray = response.getJSONArray("articles");
                        Log.e("e1",""+jsonArray.toString());
                        List<NewsApiOrgCard> topHeadlinesArray = new ArrayList<>();
                        for(int i=0;i<jsonArray.length();i++){
                            JSONObject headline = jsonArray.getJSONObject(i);
                            NewsApiOrgCard card = new NewsApiOrgCard();
                            card.setAuthor(headline.getString("author"));
                            card.setDescription(headline.getString("description"));
                            card.setTitle(headline.getString("title"));
                            card.setUrl(headline.getString("url"));
                            card.setUrlToImage(headline.getString("urlToImage"));
                            card.setPublishedAt(headline.getString("publishedAt"));
                            topHeadlinesArray.add(card);
                        }

                        swipeStack.setAdapter(new NewsApiOrgSwipeAdapter(topHeadlinesArray));
                        }catch (JSONException e){
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO: Handle error

                }
            });

     // Add the request to the RequestQueue.
    // Access the RequestQueue through your singleton class.
    MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}

public class NewsApiOrgSwipeAdapter extends BaseAdapter {
private List mData ;
private Context context ;

public NewsApiOrgSwipeAdapter(List<NewsApiOrgCard> data) {
    this.mData = data;
}

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

}

@Override
public NewsApiOrgCard getItem(int position) {
    return mData.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.news_api_org_news_card, parent, false);
    TextView stackCardTitle = (TextView) convertView.findViewById(R.id.stackcardtitle);
    TextView stackCardDescription = (TextView) convertView.findViewById(R.id.stackcarddescription);
    TextView stackCardAuthor = (TextView) convertView.findViewById(R.id.stackcardauthor);
    TextView stackCardPublishedAt = (TextView) convertView.findViewById(R.id.stackcardpublishedat);
    NewsApiOrgCard newsApiOrgCard = mData.get(position);
    stackCardTitle.setText(newsApiOrgCard.getTitle());
    stackCardDescription.setText(newsApiOrgCard.getDescription());
    stackCardAuthor.setText(newsApiOrgCard.getAuthor());
    stackCardPublishedAt.setText(newsApiOrgCard.getPublishedAt());
    return convertView;
}

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant