它没有显示我在restoList中设置的默认数据。我只是想跟踪https://www.androidhive.info/2016/01/android-working-with-recycler-view/中的Recycler View示例 我认为我的代码现在有些相同,但它仍然无法正常工作。
package com.example.jmcervantes02.jan_24;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;
public class Recycler_Activity extends AppCompatActivity {
private ArrayList<RestaurantModel> restoList = new ArrayList<>();
private RecyclerView recyclerView;
private RestaurantAdapter restoAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
restoAdapter = new RestaurantAdapter(restoList);
RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(rLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(restoAdapter);
this.createDefaultRestaurants();
}
public void createDefaultRestaurants(){
ArrayList<RestaurantModel> restoList = new ArrayList<RestaurantModel>();
restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
restoAdapter.notifyDataSetChanged();
for(int i = 0; i < restoList.size(); i++){
String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
Log.v("Restaurant", restLog);
}
String count = String.valueOf(restoAdapter.getItemCount());
Log.v("Count", count);
}
}
适配器类
package com.example.jmcervantes02.jan_24;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import junit.framework.Test;
import java.util.ArrayList;
public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.MyViewHolder> {
private ArrayList<RestaurantModel> restoList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, desc, weight;
public MyViewHolder(View view){
super(view);
name = (TextView)view.findViewById(R.id.name_text);
desc = (TextView)view.findViewById(R.id.desc_text);
weight = (TextView)view.findViewById(R.id.weight_text);
}
}
public RestaurantAdapter(ArrayList<RestaurantModel>restoList){
this.restoList = restoList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_view, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position){
RestaurantModel resto = restoList.get(position);
holder.name.setText(resto.getName());
holder.desc.setText(resto.getDescription());
holder.weight.setText(resto.getWeight());
}
@Override
public int getItemCount(){
return restoList.size();
}
}
主视图和回收器项的xml也与示例
相同答案 0 :(得分:0)
像流动的流程一样更改代码
ArrayList<RestaurantModel> restoList; // declare global
设置值后调用适配器
this.createDefaultRestaurants();
restoAdapter = new RestaurantAdapter(restoList);
RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(rLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(restoAdapter);
你的方法
public void createDefaultRestaurants(){
restoList= new ArrayList<RestaurantModel>();
restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
restoAdapter.notifyDataSetChanged();
for(int i = 0; i < restoList.size(); i++){
String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
Log.v("Restaurant", restLog);
}
String count = String.valueOf(restoAdapter.getItemCount());
Log.v("Count", count);
}
答案 1 :(得分:0)
在 Recycler_Activity.java 中根据您的代码首先调用适配器Constructor
,然后将数据设置为POJO/Model
类,将数据设置为{{1}在调用Adapter之前。
POJO
答案 2 :(得分:0)
对全球arraylist的引用
private ArrayList<RestaurantModel> restoList = new ArrayList<>();
和当地的arraylist不同
从createDefaultRestaurants中移除private ArrayList<RestaurantModel> restoList = new ArrayList<>();
,如下所示:
public void createDefaultRestaurants(){
restoList = new ArrayList<>();
restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
restoAdapter.notifyDataSetChanged();
for(int i = 0; i < restoList.size(); i++){
String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
Log.v("Restaurant", restLog);
}
String count = String.valueOf(restoAdapter.getItemCount());
Log.v("Count", count);
}
答案 3 :(得分:0)
无需在函数createDefaultRestaurants()
中重新创建public void createDefaultRestaurants(){
restoList.clear();
restoList = new ArrayList<>();
restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
//
// added model to list
//
restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
restoAdapter.notifyDataSetChanged();
for(int i = 0; i < restoList.size(); i++){
String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
Log.v("Restaurant", restLog);
}
String count = String.valueOf(restoAdapter.getItemCount());
Log.v("Count", count);
}
的对象,只需清除列表并重新分配即可使用
SqlQuery = "select * from c where c.accountId={accountId}"
答案 4 :(得分:0)
您需要将新列表提供给适配器,然后调用restoAdapter.notifyDataSetChanged();如果不向适配器提供新列表,notifyDataSetChanged将不执行任何操作。
在适配器中添加一个函数,它将list作为参数并将其放在当前的适配器列表中,然后调用notifydatasetchanged。
示例:在您的adatper
中创建此功能public void refreshList(List<Resto> restoList){
this.restoList = restoList;
notifyDataSetChanged();
}
而不是restoAdapter.notifyDataSetChanged(); ,调用restoAdapter.refreshList(restoList);