适配器类:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public List<Category> mDataset;
public Context mContext ;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView;public TextView mTextView1;
public ViewHolder(View v) {
super(v);
mTextView = (TextView)v.findViewById(R.id.title);
mTextView1=(TextView)v.findViewById(R.id.image);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<Category> myDataset) {
this.mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Word1 currentAndroidFlavor = getItem(position);
// - get element from your dataset at this position
holder.mTextView.setText(mDataset.get(position).getCategories().getName());
Log.v("check_value",mDataset.get(0).getCategories().getName()+"_value")
/*holder.mTextView1.*/;
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}
服务类:
public interface APIService {
@GET("categories")
Call<Categories> getRestaurant(@Header("Content-Type")
String contentType, @Header("user-key") String userKey);
}
主要活动:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView ;
private RecyclerView.LayoutManager mLayoutManager ;
private MyAdapter mAdapter ;
private DownloadFilesTask mDownloadFilesTask ;
private List<Category> categoryList = new ArrayList<>();
@Override
protected void onPause() {
super.onPause();
if (mDownloadFilesTask!=null && !mDownloadFilesTask.isCancelled())
{
mDownloadFilesTask.cancel(true);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* mRecyclerView = (RecyclerView)findViewById(R.id.rvCategories);
mLayoutManager= new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
mDownloadFilesTask = new DownloadFilesTask();
mDownloadFilesTask.execute();
*/
mRecyclerView = (RecyclerView) findViewById(R.id.rvCategories);
//categoryList.add(new Category());
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
mDownloadFilesTask = new DownloadFilesTask();
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mDownloadFilesTask.execute();
mAdapter = new MyAdapter(categoryList);
mRecyclerView.setAdapter(mAdapter);
// specify an adapter (see also next example)
/* mAdapter = new MyAdapter(categoryList);
mRecyclerView.setAdapter(mAdapter);
*/
// mAdapter.notifyDataSetChanged();
// specify an adapter (see also next example)
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://developers.zomato.com/api/v2.1/").
addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<Categories> peopleCall = service.getRestaurant("application/json","3a20a689dce585e6cb54bd2e88c41b43");
peopleCall.enqueue(new Callback<Categories>() {
@Override
public void onResponse(Response<Categories> response, Retrofit retrofit) {
Log.d("onResponse", "" + response.code() +
" response body " + response.body().getCategories().get(0).getCategories().getName() +
" responseError " + response.errorBody() + " responseMessage " +
response.message());
categoryList = response.body().getCategories();
}
@Override
public void onFailure(Throwable t) {
Log.d("onFailure", t.toString());
}
});
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// mRecyclerView.setAdapter(mAdapter);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
类别类(通过pojo生成):
public class Categories {
@SerializedName("categories")
@Expose
private List<Category> categories = null;
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
类别类(通过pojo生成):
public class Category {
@SerializedName("categories")
@Expose
private Categories_ categories;
public Categories_ getCategories() {
return categories;
}
public void setCategories(Categories_ categories) {
this.categories = categories;
}
}
categories_ class(通过pojo生成):
public class Categories_ {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:0)
首先,您不需要创建AsyncTask
来进行改装请求.... peopleCall.enqueue
将启动异步请求。然后,为了确保在获得categoryList
响应时更新RecyclerView,您需要在适配器上调用notifyDataSetChanged
。