2017-10-05 96 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

相關問題