Skip to content

Instantly share code, notes, and snippets.

@burnix
Created October 30, 2015 16:16
Show Gist options
  • Save burnix/287d9f8017f39fa821d1 to your computer and use it in GitHub Desktop.
Save burnix/287d9f8017f39fa821d1 to your computer and use it in GitHub Desktop.
public interface Api {
@GET("all/1.json?limit=12&offset=2&api-key=dc9e709753f2075bbb6c06a4c3fe1344%3A2%3A73339605")
public void getData(Callback<List<Result>> response);
}
public class Adapter extends ArrayAdapter<Result>{
String url = "http://api.nytimes.com/svc/news/v3/content/all/";
private Context context;
private List<Result> modelList;
public Adapter(Context context, int resource, List<Result> objects) {
super(context, resource, objects);
this.context = context;
this.modelList = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item,parent,false);
Result result = modelList.get(position);
TextView title = (TextView) view.findViewById(R.id.title);
TextView byline = (TextView) view.findViewById(R.id.byline);
TextView text = (TextView) view.findViewById(R.id.text);
title.setText(result.getTitle());
byline.setText(result.getByline() + " " + result.getUpdatedDate());
return view;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://api.nytimes.com/svc/news/v3/content/all/").build();
Api api = restadapter.create(Api.class);
api.getData(new Callback<List<Result>>() {
@Override
public void success(List<Result> result, Response response) {
resultList = result;
Adapter adapter = new Adapter(getApplicationContext(),R.layout.item,resultList);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment