我一直在閱讀不同的答案在這裏stackoverflow,並試圖實施他們的解決方案,但我仍然收到錯誤: RecyclerView:沒有適配器連接;跳過佈局RecyclerView:沒有附加適配器;跳過佈局 - 但適配器已設置
- 我花了幾天試圖排除故障,但沒有運氣任何幫助將不勝感激。
public class GaneshMainActivity extends AppCompatActivity {
private static final String URL_DATA = "https://mantraapp.000webhostapp.com/tag/ganesh-mantra?json=1";
private RecyclerView recyclerView;
private GaneshMyAdapter adapter;
private List <GaneshListitem> ganeshListitem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ganesh_mantra);
recyclerView = (RecyclerView) findViewById(R.id.ganesh_recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ganeshListitem = new ArrayList < >();
load_data_from_server();
}
private void load_data_from_server() {
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA,
new Response.Listener <String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("Ganesha Mantra");
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
GaneshListitem data = new GaneshListitem(
object.getString("title"),
object.getString("content"),
object.getString("thumbnail")
);
ganeshListitem.add(data);
}
} catch (JSONException e) {
System.out.println("No content");
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
我不知道我做錯了下面是GaneshMyadpater.java文件
public class GaneshMyAdapter extends RecyclerView.Adapter <GaneshMyAdapter.ViewHolder> {
private List <GaneshListitem> GaneshlistItems;
private Context context;
public GaneshMyAdapter(Context context, List <GaneshListitem> my_data) {
this.context = context;
this.GaneshlistItems = my_data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(GaneshlistItems.get(position).getHead());
holder.content.setText(GaneshlistItems.get(position).getDesc());
}
@Override
public int getItemCount() {
return GaneshlistItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView content;
public ImageView thumbnail;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.textviewHead);
content = (TextView) itemView.findViewById(R.id.textviewDesc);
thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
}
}
}
ñ o,適配器沒有設置。 – EpicPandaForce