2017-10-05 93 views
-1

我一直在阅读不同的答案在这里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); 
     } 
    } 
} 
+0

ñ o,适配器没有设置。 – EpicPandaForce

回答

3

onCreate

adapter = new GaneshAdapter(this , ganeshListitem); 
recyclerView.setAdapter(adapter); 

添加此行,并呼吁notifyDataSetChanged()当项目被添加到您的列表

adapter.notifyDataSetChanged(); 

这将在的onCreate()新数据

+0

我已经在上面的行中添加了,但是仍然出现一些错误 – MikeyV

+0

什么错误?您必须在初始化列表后添加此代码。如果你在此之前调用它,那么它会抛出错误。 –

+0

RecyclerView:没有附加适配器;跳过布局是错误消息,我已添加适配器=新GaneshMyAdapter(this,ganeshListitem); onCreate中的recyclerView.setAdapter(适配器)并添加了adapter.notifyDataSetChanged();当项目添加到我的列表中时 – MikeyV

0
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

0

添加下面的“

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(); 
0

你需要实现这样

    你必须采取 arrayList type hashmap
  • initialise他们ArrayList<HashMap<String,String>> allValues=new ArrayList<>();
  • ,那么你已经到putresponse值在你的HashMap这样
  • 将此内容添加到您的oncreate

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")); 
} 

竭诚为您服务

+0

在以下代码行中出现错误“无法解析符号”上下文“”适配器= new GaneshMyAdapter(context,allValues); – MikeyV

+0

让你在你的活动或这样的片段中删除这个 '上下文环境',并且在你的** Oncreate **'context = Actvity.this'和''fragment'的'Context = getAcitivity' ...如果有也更新了我的代码,请通过它 –

+0

是的,我仍然得到错误 – MikeyV

相关问题