我已经尝试了所有教程来解决我的问题,但是都没有给出解决方案。这是我的代码:
霹雳巴士活动班
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pilih_bus);
mRecycler = (RecyclerView) findViewById(R.id.recyclerView1);
mManager = new LinearLayoutManager(this);
mRecycler.setLayoutManager(mManager);
mRecycler.setHasFixedSize(true);
api = ApiClient.getApiClient().create(ApiBus.class);
Call<ResponseModel> getdata = api.getBus();
getdata.enqueue(new Callback<ResponseModel>() {
@Override
public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
Log.d("RETROFIT", "RESPONSE : " + response.body().getmKode());
mItems = response.body().getResult();
mAdapter = new BusAdapter(PilihBusActivity.this, mItems);
mRecycler.setAdapter(mAdapter);
}
@Override
public void onFailure(Call<ResponseModel> call, Throwable t) {
Log.d("RETROFIT", "FAILED : respon gagal");
}
});
我不知道障碍的原因和障碍,但让我向您展示我的适配器类,
这是我的适配器类:
public class BusAdapter extends
RecyclerView.Adapter<BusAdapter.ViewHolders> {
List<Bus> buses;
Context ctx;
public BusAdapter(Context ctx, List<Bus> buses) {
this.buses = buses;
this.ctx = ctx;
}
public void setmList(List<Bus>mList){
this.buses = mList;
notifyDataSetChanged();
}
@NonNull
@Override
public BusAdapter.ViewHolders onCreateViewHolder( ViewGroup parent, int viewType) {
View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_pilih_bus, parent, false);
ViewHolders holder = new ViewHolders(layout);
return holder;
}
@Override
public void onBindViewHolder( BusAdapter.ViewHolders holder, final int position) {
// holder.dari_id.setText(mList.get(position).getDari_id());
// holder.ke_id.setText(mList.get(position).getKe_id());
holder.class_name.setText(buses.get(position).getClass_name());
holder.price.setText(buses.get(position).getPrice());
holder.time_dari.setText(buses.get(position).getTime_dari());
}
@Override
public int getItemCount() {
return buses.size();
}
public class ViewHolders extends RecyclerView.ViewHolder {
TextView class_name, price, time_dari;
ImageView image;
public ViewHolders( View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
class_name = (TextView) itemView.findViewById(R.id.class_name);
price = (TextView)itemView.findViewById(R.id.price);
time_dari = (TextView) itemView.findViewById(R.id.time_dari);
}
}
我遇到了同样的问题“没有连接的适配器跳过布局”,并且已经设置了适配器。
请帮助我,谢谢你
最佳答案
这样尝试
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pilih_bus);
mRecycler = (RecyclerView) findViewById(R.id.recyclerView1);
mManager = new LinearLayoutManager(this);
mRecycler.setLayoutManager(mManager);
mRecycler.setHasFixedSize(true);
// add below lines here
mItems = new ArrayList<>(); // initialize your mItems here
mAdapter = new BusAdapter(this, mItems); // initialize your adapter with empty mItems
mRecycler.setAdapter(mAdapter); // set adapter to your recyclerview like this
api = ApiClient.getApiClient().create(ApiBus.class);
Call<ResponseModel> getdata = api.getBus();
getdata.enqueue(new Callback<ResponseModel>() {
@Override
public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
Log.d("RETROFIT", "RESPONSE : " + response.body().getmKode());
if(response.isSuccessful()){ // you should check your response is ok or not before doing further action with the response.
mItems.clear(); // this will clear all privous data from mItmes
mItems.addAll(response.body().getResult()); // add all data to your list
mAdapter.notifyDataSetChanged(); // this will notify your adapter that your data set has been changed.
}else{
// handle if your api response is not okay
}
}
@Override
public void onFailure(Call<ResponseModel> call, Throwable t) {
Log.d("RETROFIT", "FAILED : respon gagal");
}
});
关于java - RecyclerView Adapter附加的跳过布局,为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59525800/