RecyclerView:没有附加适配器;跳过布局 - 但适配器已设置
问题描述:
我一直在阅读不同的答案在这里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);
}
}
}
答
在onCreate
adapter = new GaneshAdapter(this , ganeshListitem);
recyclerView.setAdapter(adapter);
添加此行,并呼吁notifyDataSetChanged()
当项目被添加到您的列表
adapter.notifyDataSetChanged();
这将在的onCreate()新数据
答
ganeshListitem = new ArrayList < >();
adapter = new GaneshAdapter(this , ganeshListitem);
recyclerView.setAdapter(adapter);
load_data_from_server();
做出上述改变
adapter.addData(ganeshListitem)
添加上述行ganeshListitem后更新recycleview。添加(数据);
public void addData(List<GaneshListitem> list){
GaneshlistItems.addAll(list);
notifyDataSetChanged()
}
这上面的方法添加到您的适配器类
+0
谢谢你的建议,但RecyclerView:没有适配器连接;跳过布局错误仍然存在 – MikeyV
答
添加下面的“
recyclerView.setLayoutManager(new LinearLayoutManager(this));
”这样的代码:
recycleView. setAdapter(adapter);
并添加下面的“
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("Ganesha Mantra");
for (int i = 0; 1 < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
GaneshListitem data = new GaneshListitem(
object.getString("title"),
object.getString("content"),
object.getString("thumbnail")
);
ganeshListitem.add(data);
}
验证码
“:
adapter. notifyDataSetChanged();
答
你需要实现这样
-
你必须采取
- 和
initialise
他们ArrayList<HashMap<String,String>> allValues=new ArrayList<>();
- ,那么你已经到
put
您response
值在你的HashMap这样 - 将此内容添加到您的
oncreate
arrayList type hashmap
context = getActivity();
RecyclerView.LayoutManager recyclerViewLayoutManager = new LinearLayoutManager(context);
rv_lunch_break.setLayoutManager(recyclerViewLayoutManager);
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);
HashMap<String,String> hashMap=new HashMap<>();
hashMap.put("title",object.getString("title"));
hashMap.put("content",object.getString("content"));
hashMap.put("thumbnail",object.getString("thumbnail"));
allValues.add(hashMap);
adapter=new yourAdapterName(context,allValues);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
} 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);
}
- 在你
adapter
类,你必须再次采取arrayList type hashmap
和HashMap,也initialise
他们也是这样
HashMap<String,String> hashMAp=new HashMap<>();
private ArrayList<HashMap<String,String>> allList;
- 在你
onBindViewHolder
你需要提取value
这样
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
hashMAp=allList.get(position);
holder.your_TextView_Refrence.setText(hashmap.get("title"));
holder.your_TextView_Refrence.setText(hashmap.get("content"));
}
ñ o,适配器没有设置。 – EpicPandaForce