我有一个ArrayList
,该列表从改造中获取数据及其工作正常。问题是当我尝试将列表发送到RecyclerView
适配器时,它是空的!有什么想法吗?
这是活动
public class ToDoRecycler extends AppCompatActivity {
RecyclerView todoRecyclerView;
private RecyclerView.Adapter todoAdapter;
private RecyclerView.LayoutManager todoLayoutManager;
public List<ToDo>results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_recycler);
todoRecyclerView = (RecyclerView)findViewById(R.id.todoRecyclerView);
todoRecyclerView.setHasFixedSize(true);
results= new ArrayList<ToDo>();
todoLayoutManager = new LinearLayoutManager(this);
todoRecyclerView.setLayoutManager(todoLayoutManager);
todoRecyclerView.setAdapter(todoAdapter);
todoAdapter = new TodoRecyclerAdapter(this,results);
getRetrofitObject();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ToDoRecycler.this,AddToDo.class);
startActivity(intent);
}});}
public void getRetrofitObject() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<Result> call = service.getresults();
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
results = response.body().getResults();
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.d("onFailure", t.toString());
}});}
}
适配器
public class TodoRecyclerAdapter extends RecyclerView.Adapter<TodoRecyclerAdapter.ViewHolder> {
static List<ToDo> todoResults;
static Context context;
List<ToDo>results;
public TodoRecyclerAdapter(List<ToDo> results) {
this.todoResults = results;
}
@Override
public TodoRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_items, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(final TodoRecyclerAdapter.ViewHolder holder, final int position) {
}
@Override
public int getItemCount() {
return todoResults.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView todoTitle;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
todoTitle = (TextView) itemLayoutView.findViewById(R.id.todo_title);
}
}
}
答案 0 :(得分:0)
替换这两行
todoRecyclerView.setAdapter(todoAdapter);
todoAdapter = new TodoRecyclerAdapter(this,results);
到此:
todoAdapter = new TodoRecyclerAdapter(this,results);
todoRecyclerView.setAdapter(todoAdapter);
答案 1 :(得分:0)
首先构建适配器,然后将适配器添加到RecyclerView
todoAdapter = new TodoRecyclerAdapter(this,results);
todoRecyclerView.setAdapter(todoAdapter);
然后,您需要通知适配器您正在引用的数据集中的更改
public void getRetrofitObject() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<Result> call = service.getresults();
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
results = response.body().getResults();
// Call notifyDataSetChanged here
todoAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.d("onFailure", t.toString());
}});}
<强>更新强>
因此,在第一次设置results
时,显然是null
,因为其中没有数据。您需要在TodoRecyclerAdapter
中处理此问题。您只需检查null
函数中的getCount
值,然后像这样返回0
public int getCount() {
if(results != null) return results.size();
else return 0;
}
答案 2 :(得分:0)
问题是您在改造结果之前创建/调用适配器......!
您应该等待结果,当您得到结果时,初始化并将适配器设置为回调中的recyclerview ...!
使用以下代码获取帮助,
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
results = response.body().getResults();
todoRecyclerView.setAdapter(new TodoRecyclerAdapter(this,results));
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.d("onFailure", t.toString());
}});}
并从onCreate()方法
中删除适配器初始化<强>更新强> 您的活动类应如下所示,
public class ToDoRecycler extends AppCompatActivity {
RecyclerView todoRecyclerView;
private RecyclerView.Adapter todoAdapter;
private RecyclerView.LayoutManager todoLayoutManager;
public List<ToDo>results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_recycler);
todoRecyclerView = (RecyclerView)findViewById(R.id.todoRecyclerView);
todoRecyclerView.setHasFixedSize(true);
results= new ArrayList<ToDo>();
todoLayoutManager = new LinearLayoutManager(this);
todoRecyclerView.setLayoutManager(todoLayoutManager);
getRetrofitObject();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ToDoRecycler.this,AddToDo.class);
startActivity(intent);
}});}
}
public void getRetrofitObject() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<Result> call = service.getresults();
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
results = response.body().getResults();
todoRecyclerView.setAdapter(new TodoRecyclerAdapter(ActivityName.this,results));
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.d("onFailure", t.toString());
}});}
}
答案 3 :(得分:0)
看起来你的onBindViewHolder方法缺少一个实现,你的ViewHolder构造函数不会返回一个ViewHolder ..