我在fragmnet中使用recyclerview。
Fragmnet正在工作,但我的回收查看數據沒有顯示。
我的XML代碼:onCreateViewHolder不在fragmnet recyclerview中調用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp">
<LinearLayout android:id="@+id/projectcommends" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" android:padding="@dimen/common_padding">
<TextView style="@style/TextHeading" android:text="Project comments" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp">
<TextView android:layout_width="180dp" android:layout_height="wrap_content" android:text="Date" android:textSize="17dp" android:textColor="@color/black" android:gravity="center_horizontal" android:textStyle="bold" />
<TextView android:layout_width="180dp" android:layout_height="wrap_content" android:text="Comments" android:textSize="17dp" android:textColor="@color/black" android:gravity="center_horizontal" android:textStyle="bold" />
</LinearLayout>
<android.support.v7.widget.RecyclerView android:id="@+id/commendsList" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
行XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_marginTop="5dp">
<TextView android:id="@+id/commentdate" android:layout_width="180dp" android:textSize="15dp" android:layout_height="wrap_content" android:text="20-06-2016" android:gravity="center" />
<TextView android:id="@+id/commentdetail" android:layout_width="180dp" android:layout_height="wrap_content" android:textSize="15dp" android:text="project commends testing list" android:gravity="center" />
我Fragmnet代碼:
public class ProjectComments extends Fragment {
private List <CommentsData> arraylist;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private String ordered;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.project_comments, container, false);
initialize(view);
Bundle b = getArguments();
orderid = b.getString("orderid");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
arraylist = new ArrayList < >();
adapter = new CommentsAdapter(arraylist);
recyclerView.setAdapter(adapter);
getCommentsData();
return view;
}
private void initialize(View view) {
recyclerView = (RecyclerView) view.findViewById(R.id.commendsList);
}
private void getCommentsData() {
String mobilecustomertoken = SharedPreferencesManager.readPreferenceString("MobileCustomerToken", "D/N");
JSONObject commentData = new JSONObject();
try {
commentData.put("mobilecustomertoken", mobilecustomertoken);
commentData.put("orderid", orderid);
JsonObjectRequest commentsObject = new JsonObjectRequest(1, Common.CommentsList, commentData, new Response.Listener <JSONObject>() {
@Override
public void onResponse(JSONObject commentsResponse) {
Log.d("Responsedetail", commentsResponse.toString());
try {
int status = commentsResponse.getInt("status");
if (status == 1) {
commentsProgress(commentsResponse);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("Response", "PENDING ERROR");
}
});
commentsObject.setShouldCache(false);
ServiceBellApp.getInstance().addToRequestQueue(commentsObject);
} catch (JSONException localJSONException) {
localJSONException.printStackTrace();
return;
}
}
private void commentsProgress(JSONObject commentsResponse) throws JSONException, InterruptedException {
JSONArray result = commentsResponse.getJSONArray("response");
CommentsData orderListModule;
for (int i = 0; i < result.length(); i++) {
JSONObject jsonObject = result.getJSONObject(i);
orderListModule = new CommentsData();
orderListModule.setCommentsData(jsonObject.getString("commentdate"));
orderListModule.setCommentsDetail(jsonObject.getString("comendDetail"));
arraylist.add(orderListModule);
}
adapter.notifyDataSetChanged();
int count = adapter.getItemCount();
if (count != 0) {
Log.d("Response", "Test");
}
}
}
最後我的適配器代碼:
public class CommentsAdapter extends RecyclerView.Adapter <CommentsAdapter.RecyclerViewHolder> {
List <CommentsData> arrayList;
public CommentsAdapter(List <CommentsData> list) {
this.arrayList = list;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comments_list, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
CommentsData commentsData = arrayList.get(position);
Log.d("ResponseAdapter", commentsData.getCommentsData());
holder.commentdate.setText(commentsData.getCommentsData());
holder.commentDetail.setText(commentsData.getCommentsDetail());
}
@Override
public int getItemCount() {
String dd = Integer.toString(arrayList.size());Log.d("Responsecount", dd);return (null != arrayList ? arrayList.size() : 0);
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView commentdate, commentDetail;
public RecyclerViewHolder(View itemView) {
super(itemView);
Log.d("ResponseAda", "Success");
commentdate = (TextView) itemView.findViewById(R.id.commentdate);
commentDetail = (TextView) itemView.findViewById(R.id.commentdetail);
}
}
}
編輯:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.project_comments, container, false);
initialize(view);
Bundle b = getArguments();
orderid = b.getString("orderid");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new
LinearLayoutManager(getActivity()));
arraylist = new ArrayList < >();
getCommentsData();
adapter = new CommentsAdapter(arraylist);
recyclerView.setAdapter(adapter);
return view;
}
任何一個請幫我爲什麼我的onCreateViewHolder功能沒有要求?
你有數據在你正在設置的urll適配器arraylist? – Raghavendra
是的,我有arraylist數據。 getCount是1 – vinoth12594
你可以試試這個recyclerView.setAdapter(adapter);在調用adapter.notifyDatasetChanged並嘗試一次之前放這條線 – Raghavendra