gpt4 book ai didi

java - RecyclerView不显示数据

转载 作者:行者123 更新时间:2023-12-01 16:46:46 25 4
gpt4 key购买 nike

我是 android 新手,正在开发 android 应用程序,但我停留在这里。 RecyclerView 显示为空。日志中没有错误或崩溃。我删除并创建了新的但相同的问题。我找不到问题所在。

主要 Activity 的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);


}
}
}

最佳答案

您必须添加 LayoutManager到 RecyclerView 。使用setLayoutManager (RecyclerView.LayoutManager layout)方法将 LayoutManager 设置为 RecyclerView。如果您没有设置任何LayoutManager,您将看不到任何数据。

LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);

关于java - RecyclerView不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49045970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com