[英]Values not showing in android recyclerview
我的項目中有一個recyclerview,數據未綁定到recyclerview。 我正在從服務器獲取數據,並且數據正確到來。 我在適配器類中放入了Toast,它正在工作。 我無法弄清楚問題。
活動課...
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>
在您的活動中,在此行之前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();
這會讓您如雨后春筍般得到結果
從服務器加載數據后,更新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);
我認為這是因為您在創建對象之前已經設置了適配器。
recyclerView.setAdapter(adcAdapter);
getData();
首先,將適配器設置為recyclerView,然后調用在其中創建adcAdapter對象的getData()方法。
您的適配器具有兩個參數context和arrayList,因此在創建適配器實例時,請確保將兩個值都傳遞給適配器,並將相同的適配器設置為RecyclerView。
mAdapter = new MAdapter(this,list); mRecyclerView.setAdapter(mAdapter);
一旦列表中有數據,然后將該列表傳遞給適配器。
您需要在初始化之前將adcAdapter
設置為recyclerView
。
更改代碼為打擊。
adcAdapter = new ViewDealerComplainAdapter(ViewDealerCompln.this, compList);
recyclerView.setAdapter(adcAdapter);
另外,您還需要在適配器上調用notifyDataSetChanged()
,以在收到響應時更新recycleview。
adcAdapter.notifyDataSetChanged();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.