我在MainActivity中有一个名为CustomAdapter的recyclerView适配器,我通过解析JSON来获取数据,并且该数据用于设置CustomAdapter类中的textview。
启动我的应用程序时,我没有显示recyclerView列表,也没有调用适配器。所以我的问题是我无法调用customAdapter的函数,例如onBindViewHolder,onCreateViewHolder。
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
Context context;
RealmResults<Film> realmResults;
View.OnTouchListener listener;
Realm realm;
public CustomAdapter(Context context, View.OnTouchListener listener) {
this.context = context;
this.listener = listener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_liste, parent, false);
MyViewHolder viewHolder = new MyViewHolder(root);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
Realm.init(context);
RealmResults<Film> films = realm.where(Film.class).findAll();
Film f = films.get(position);
for(film film:filmler) {
try {
holder.title.setText(film.getTitle());
holder.year.setText(film.getYear());
holder.poster.setImageURI(film.getPoster());
if (film.getPoster().toString().equals("N/A") == true) {
Picasso.get().load(R.drawable.noImage).fit().into(holder.image);
} else {
Picasso.get().load(film.getPoster()).into(holder.image);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
if(realmResults==null)
{
return 0;
}
return realmResults.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnTouchListener {
public TextView title, year;
public ImageView image;
public LinearLayout linearLayout;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title_id);
year = itemView.findViewById(R.id.year_id);
image = itemView.findViewById(R.id.image_id);
linearLayout = itemView.findViewById(R.id.linearlayout_id);
linearLayout.setOnTouchListener(listener);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent.getAction() == MotionEvent.ACTION_SCROLL){
return true;
}
return false;
}
}
}
我的MainActivity类:
final StringRequest stringRequest1 = new StringRequest(Request.Method.GET, url1, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
JSONObject jsonObject1 = new JSONObject(response);
String response = jsonObject1.getString("Response");
if (response.equals("True") && jsonObject1.has("Search")==true) {
JSONArray array1 = jsonObject1.getJSONArray("Search");
for (int i = 0; i < array1.length(); i++) {
final JSONObject films = array1.getJSONObject(i);
realm1.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm1) {
Film film = realm1.createObject(Film.class);
try {
film.setTitle(filmler.getString("Title"));
film.setYear(filmler.getString("Year"));
film.setPoster(filmler.getString("Poster"));
realm1.copyToRealm(film);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
for (int j = 0; j < array1.length(); j++) {
array1.remove(j);
}
adapter = new CustomAdapter(this, this);
recycle1.setAdapter(adapter);
答案 0 :(得分:2)
您在onBindViewHolder中出错。 尝试在构造函数中传递列表
public CustomAdapter(Context context, View.OnTouchListener
listener,RealmResults<film> realmResults) {
this.context = context;
this.listener = listener;
this.realmResults = realmResults;
}
并从bindViewHolder删除此代码
Log.d(TAG, "onBindViewHolder : called.");
Realm.init(context);
//film filmler = filmList.get(position);
//realm = Realm.getDefaultInstance();
RealmResults<film> filmler =
realm.where(film.class).findAll();
film f = filmler.get(position);
不要在onBindViewHolder中为设置数据调用循环,只需遵循
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int
position) {
film item = realmResults.get(position);
if(item!=null && item.isValid()){
holder.baslik.setText(item.getTitle());
holder.yil.setText(item.getYear());
holder.resim.setImageURI(item.getPoster());
if (item.getPoster().toString().equals("N/A")) {
Picasso.get().load(R.drawable.resimyok).fit().into(holder.resim);
} else {
Picasso.get().load(film.getPoster()).into(holder.resim);
}
}
}
在下面修复此问题以获得响应并将其保存到领域
JSONArray array1 = jsonObject1.getJSONArray("Search");
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.createOrUpdateAllFromJson(filn.class, array1);
realm.commitTransaction();
//get all saved data from realm
RealmResults<T> results =
realm.where(film.class).query.findAll();
adapter = new
CustomAdapter(getApplicationContext(),MainActivity.this,results);
recycle1.setAdapter(adapter);
arama.getText().clear();