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

Not working with object list #63

Description

@Durgaprasad1541996

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

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions