我是android新手,正在开发一个Android应用程序,但我卡在这里。 RecyclerView显示为空。日志中没有错误或崩溃。我删除并创建了新的但同样的问题。我找不到什么是错的。
主要活动的java代码
public class HomeActivity extends AppCompatActivity {
private RecyclerView rcview1;
private RecyclerView.Adapter cmpadapter1;
private List<commdtylist> listitems2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
rcview1 = (RecyclerView) findViewById(R.id.commoditylist);
rcview1.setItemAnimator(new DefaultItemAnimator());
listitems2 = new ArrayList<>();
rcview1.setHasFixedSize(true);
viewcmp();
}
public void viewcmp() {
StringRequest stringRequest=new StringRequest(Request.Method.POST, otpur, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jobj = new JSONObject(response);
JSONArray jsonArray = jobj.getJSONArray("Details");
for (int i = 0; i < jsonArray.length(); i++) {
jobj = jsonArray.getJSONObject(i);
commdtylist item = new commdtylist(
jobj.getString("quantity_entitle"),
jobj.getString("comm_code"),
jobj.getString("item_description")
);
listitems2.add(item);
}
cmpadapter1 = new CommdityviewAdapter(listitems2, getApplicationContext());
rcview1.setAdapter(cmpadapter1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> param = new HashMap<>();
param.put("rc_no", crd);
return param;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
commdtylist
public class commdtylist {
private String quantity_entitle;
private String comm_code;
private String item_description;
public commdtylist(String quantity_entitle, String comm_code, String item_description) {
this.quantity_entitle = quantity_entitle;
this.comm_code = comm_code;
this.item_description = item_description;
}
public String getquantity_entitle() {
return quantity_entitle;
}
public String getcomm_code() {
return comm_code;
}
public String getitem_description() {
return item_description;
}
}
适配器
public class CommdityviewAdapter extends RecyclerView.Adapter<CommdityviewAdapter.RViewHolder> {
private List<commdtylist> listitems2;
private Context context;
int lastPosition = -1;
public CommdityviewAdapter(List<commdtylist> listitems2, Context context) {
this.listitems2 = listitems2;
this.context = context;
}
public static Toast t = null;
@Override
public RViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.view_cmdstyle,parent,false);
return new RViewHolder(v);
}
@Override
public void onBindViewHolder(final RViewHolder holder,final int position) {
final commdtylist listitem=listitems2.get(position);
holder.quantity_entitle.setText(listitem.getquantity_entitle());
holder.comm_code.setText(listitem.getcomm_code());
holder.item_description.setText(listitem.getitem_description());
setAnimation(holder.itemView, position);
}
private void setAnimation(View itemView, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition)
{
Animation animation = AnimationUtils.loadAnimation(itemView.getContext(), R.anim.up_from_bottom);
itemView.startAnimation(animation);
lastPosition = position;
}
else{
Animation animation = AnimationUtils.loadAnimation(itemView.getContext(), R.anim.down_from_top);
//android.R.anim.slide_in_left
itemView.startAnimation(animation);
lastPosition = position;
}
}
@Override
public int getItemCount() {
return listitems2.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
public class RViewHolder extends RecyclerView.ViewHolder{
public TextView quantity_entitle,comm_code,item_description;
public RViewHolder(View itemView) {
super(itemView);
quantity_entitle=(TextView)itemView.findViewById(R.id.quantity_entitle);
comm_code=(TextView)itemView.findViewById(R.id.comm_code);
item_description=(TextView)itemView.findViewById(R.id.item_description);
}
}
}
答案 0 :(得分:1)
您必须将LayoutManager
添加到RecyclerView
。使用setLayoutManager (RecyclerView.LayoutManager layout)方法将LayoutManager
设置为RecyclerView
。如果您未设置任何LayoutManager
,则不会看到任何数据。
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
答案 1 :(得分:1)
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview.setLayoutManager(layoutManager);
希望能帮到你。
答案 2 :(得分:1)
请使用setLayoutManager方法设置RecyclerView的layoutmanager。
答案 3 :(得分:1)
将布局管理器设置为recylerView
IO
答案 4 :(得分:1)
在家庭活动中做一些改变,比如
private CommdityviewAdapter cmpadapter1; // replace private RecyclerView.Adapter cmpadapter1;
rcview1.setLayoutManager(new LinearLayoutManager(this));
当你在recyclerview中设置适配器然后在适配器之后。
cmpadapter1.notifyDataSetChanged();