2017-06-30 90 views
0

我在我的項目中有一個recyclerview和數據不綁定到recyclerview。我正在從服務器獲取數據,並正確地發送數據。我在適配器類中放了一個Toast,它正在工作。我無法弄清楚這個問題。值不顯示在Android recyclerview

活動課....

public class ViewDealerCompln extends AppCompatActivity { 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.view_dealer_complain); 

     typeface = Typeface.createFromAsset(getAssets(), "productsans.ttf"); 

     recyclerView = (RecyclerView) findViewById(R.id.com_recyclerView); 

     compList = new ArrayList<>(); 

     toolbar = (Toolbar) findViewById(R.id.com_view_app_bar); 
     TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setTitle(""); 
     mTitle.setText("Complain History"); 
     mTitle.setTypeface(typeface); 

     repID = DealerListAdapter.getRepID(); 
     dealerID = DealerListAdapter.getDealerID(); 

     mLayoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(mLayoutManager); 

     recyclerView.setAdapter(adcAdapter); 

     getData(); 
    } 

    private void getData() { 

     String tag_string_req = "req_data"; 

     request = new StringRequest(Request.Method.POST, AppConfig.URL_JSON_GETCOMPLAIN, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 

       try { 
        JSONObject jsonObject = new JSONObject(response); 
        if (jsonObject.names().get(0).equals("feed")) { 
         JSONArray jsonFeedArray = jsonObject.getJSONArray("feed"); 
         if (jsonFeedArray.length() > 0) { 

          for (int i = 0; i < jsonFeedArray.length(); i++) { 
           JSONObject currentObject = jsonFeedArray.getJSONObject(i); 
           String namemm = currentObject.getString("compId"); 
           compList.add(namemm); 
          } 
          adcAdapter = new ViewDealerComplainAdapter(ViewDealerCompln.this, compList); 

         } else { 
          Toast.makeText(getApplicationContext(), "No data Available!", Toast.LENGTH_SHORT).show(); 
         } 

        } else { 
         Toast.makeText(getApplicationContext(), "Invalid Response from the server!", Toast.LENGTH_SHORT).show(); 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

      } 
     }) { 

     }; 

    } 

} 

適配器類...

public class ViewDealerComplainAdapter extends RecyclerView.Adapter<ViewDealerComplainAdapter.ItemViewHolder> { 

    private LayoutInflater inflater; 
    private ArrayList<String> subList; 
    private Context context; 

    public ViewDealerComplainAdapter(Context context, ArrayList<String> a) { 
     this.context = context; 
     inflater = LayoutInflater.from(context); 
     this.subList = a; 
     Toast.makeText(context, subList.toString(), Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = inflater.inflate(R.layout.custom_complain_list_row, parent, false); 
     ViewDealerComplainAdapter.ItemViewHolder holder = new ViewDealerComplainAdapter.ItemViewHolder(view); 
     return holder; 
    } 

    @Override 
    public void onBindViewHolder(ItemViewHolder holder, int position) { 
     holder.title.setText(subList.get(position)); 
    } 

    @Override 
    public int getItemCount() { 
     return subList.size(); 
    } 

    public class ItemViewHolder extends RecyclerView.ViewHolder { 
     private TextView title; 

     public ItemViewHolder(View itemView) { 
      super(itemView); 
      title = (TextView) itemView.findViewById(R.id.compHID); 
     } 
    } 
} 

自定義行佈局...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <TextView 
     android:id="@+id/compHID" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/white" 
     android:padding="10dp" /> 
</LinearLayout> 

主要佈局...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/com_view_app_bar" 
     layout="@layout/app_toolbar_send_complain"></include> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/com_recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="5dp"> 

    </android.support.v7.widget.RecyclerView> 
</LinearLayout> 

回答

1

在您的活動中,此行之前recyclerView。setAdapter(adcAdapter); 寫

adcAdapter=new ViewDealerComplainAdapter(this,new ArrayList<String>()) 

在適配器創建一個函數如下

public void setData(ArrayList<String> data){ 
this.sublist=data; 
} 

現在,在函數的getData() 這個

for (int i = 0; i < jsonFeedArray.length(); i++) { 
           JSONObject currentObject = jsonFeedArray.getJSONObject(i); 
           String namemm = currentObject.getString("compId"); 
           compList.add(namemm); 
          } 

寫後

adcAdapter.setData(compList); 
adcAdapter.notifyDataSetChanged(); 

這會讓給你結果正確雨

+0

我必須將** notifyItemRangeChanged(0,data.size()); **放入* *適配器中的setData()**方法。 –

+0

是的,如果你不想在setData之後調用adcAdapter.notifyDataSetChanged() –

0

從服務器更新RecyclerView加載數據後

adcAdapter = new ViewDealerComplainAdapter(ViewDealerCompln.this, compList); 
    adcAdapter.notifyDataSetChanged(); 

另一種方式

創建適配器的方法

public void setDataChange(List<Object> asList) { 
     this.List = asList; 
     //now, tell the adapter about the update 
     notifyDataSetChanged(); 
    } 

adcAdapter.setDataChange(list); 
-2

要設置adcAdapter在初始化之前對recyclerView進行初始化。

更改密碼。

adcAdapter = new ViewDealerComplainAdapter(ViewDealerCompln.this, compList); 
recyclerView.setAdapter(adcAdapter); 

你也需要調用notifyDataSetChanged()上適配器,當你得到響應更新recycleview。

adcAdapter.notifyDataSetChanged(); 
+0

由於OP正在發出網絡請求,所以在您調用setAdapter之後,它可能會完成並創建一個新的適配器 – Pelocho

0

這條線後設置一個適配器 -

adcAdapter = new ViewDealerComplainAdapter(ViewDealerCompln.this, compList); 
recyclerView.setAdapter(adcAdapter); 
0

我認爲這正在發生的事情,因爲你已經設置創建對象之前適配器。

recyclerView.setAdapter(adcAdapter); 
getData(); 

您首先將適配器設置爲recyclerView,然後調用getData()方法在其中創建adcAdapter的對象。

0

適配器有兩個參數方面& ArrayList中,所以在創建適配器的情況下確保你傳遞這兩個值適配器和設置相同的適配器與RecyclerView。

mAdapter = new MAdapter(this,list); mRecyclerView.setAdapter(mAdapter);

一旦您的列表中有數據,然後將該列表傳遞給適配器。