我检查了几次,但我不明白为什么不显示项目 它只是显示供Recycler视图使用的空白空间 我在日志中每处都正常的地方使用日志方法,但我无法 结果 这是我的主要活动,我叫适配器,并在其中建立阵列列表并叫出5个项目
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interviews();
}
private void interviews() {
recyclerView =findViewById(R.id.rcl_orders);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
OrderAdapter orderAdapter =new OrderAdapter(this,getOrders());
recyclerView.setAdapter(orderAdapter);
}
private ArrayList<Orders> getOrders(){
ArrayList<Orders> orders = new ArrayList<>();
orders.add(new Orders(1,1," NewYork City"));
orders.add(new Orders(2,2,"Masochist"));
orders.add(new Orders(3,1,"San francisco"));
orders.add(new Orders(4,5,"pennsylvania"));
orders.add(new Orders(5,3,"LosAngeles"));
return orders;
}
}
这是我的模特 我在该类中构建了一个模型,并创建了setter和getter,当我记录该变量时,它们是完整且正确的
public class Orders {
private int kind,count ;
private String address;
public Orders() {
}
public int getKind() {
return kind;
}
public void setKind(int kind) {
this.kind = kind;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Orders(int kind, int count, String address) {
this.kind = kind;
this.count = count;
this.address = address;
}
}
这是我的适配器
public class OrderAdapter extends RecyclerView.Adapter<OrderViewHolder> {
private Context context;
private ArrayList<Orders> orders;
public OrderAdapter(Context context, ArrayList<Orders> orders) {
this.context=context;
this.orders=orders;
}
@Override
public OrderViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ordercity,null);
OrderViewHolder orderViewHolder =new OrderViewHolder(view);
return orderViewHolder;
}
@Override
public void onBindViewHolder( OrderViewHolder holder, int position) {
Orders order = orders.get(position);
holder.textView.setText("set text ");
}
@Override
public int getItemCount() {
Log.i("MainActtivity","value"+ orders.size());
return (orders == null ? 0 : orders.size());
}
}
这是我的观点持有者
public class OrderViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public CheckBox Selected;
public OrderViewHolder(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.txt_order);
Selected=itemView.findViewById(R.id.chk_selected);
}
}
请帮助我!
答案 0 :(得分:0)
在适配器中进行如下更新
@Override
public int getItemCount() {
Log.i("MainActtivity","value"+ orders.size());
return orders.size();
}
答案 1 :(得分:0)
更改
@Override
public OrderViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ordercity,null);
OrderViewHolder orderViewHolder =new OrderViewHolder(view);
return orderViewHolder;
}
@Override
public int getItemCount() {
Log.i("MainActtivity","value"+ orders.size());
return (orders == null ? 0 : orders.size());
}
收件人
@Override
public OrderViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.ordercity, parent, false);
return new MyViewHolder(itemView);
}
@Override
public int getItemCount() {
Log.i("MainActtivity","value"+ orders.size());
return orders.size());
}
答案 2 :(得分:0)
在您的onCreateViewHolder()
中:
更改此内容:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ordercity,null);
为此:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ordercity , parent , false);