我一直在制作一个使用RecyclerView的应用程序,但是它没有显示任何内容。.为什么没有显示RecyclerView的内容。我的代码在下面
activity_history.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HistoryActivity"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:id="@+id/layout">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/historyRecyclerView"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
HistoryActivity.java
public class HistoryActivity extends AppCompatActivity {
private RecyclerView mHistoryRecyclerView;
private RecyclerView.Adapter mHistoryAdapter;
private RecyclerView.LayoutManager mHistoryLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Toast.makeText(this, "dddd", Toast.LENGTH_SHORT).show();
mHistoryRecyclerView = (RecyclerView) findViewById(R.id.historyRecyclerView);
mHistoryRecyclerView.setNestedScrollingEnabled(false);
mHistoryRecyclerView.setHasFixedSize(true);
mHistoryLayoutManager = new LinearLayoutManager(HistoryActivity.this);
mHistoryRecyclerView.setLayoutManager(mHistoryLayoutManager);
mHistoryAdapter = new HistoryAdapter(getDataSetHistory(), HistoryActivity.this);
mHistoryRecyclerView.setAdapter(mHistoryAdapter);
HistoryObject obj=new HistoryObject("12345");
resultsHistory.add(obj);
mHistoryAdapter.notifyDataSetChanged();
}
private ArrayList resultsHistory = new ArrayList<HistoryObject>();
private List<HistoryObject> getDataSetHistory() {
return resultsHistory;
}
}
HistoryAdapter.java
public class HistoryAdapter extends RecyclerView.Adapter<HistoryViewHolders> {
private List<HistoryObject> itemList;
private Context context;
public HistoryAdapter(List<HistoryObject> itemList, Context context) {
this.itemList = itemList;
this.context = context;
Toast.makeText(context,itemList.size()+"" , Toast.LENGTH_SHORT).show();
}
@NonNull
@Override
public HistoryViewHolders onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_history, null, false);
HistoryViewHolders rcv = new HistoryViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(@NonNull HistoryViewHolders holder, int position) {
holder.rideId.setText(itemList.get(position).getRideId());
Toast.makeText(context, holder.rideId.getText().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public int getItemCount() {
return 0;
}
}
HistoryObject
package com.example.ikramkhan.insta.historyRecyclerView;
public class HistoryObject {
private String rideId;
public HistoryObject(String rideId) {
this.rideId = rideId;
}
public String getRideId() {
return rideId;
}
public void setRideId(String rideId) {
this.rideId = rideId;
}
}
HistoryViewHolders.java
public class HistoryViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView rideId;
public HistoryViewHolders(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
rideId = (TextView) itemView.findViewById( R.id.rideId);
}
@Override
public void onClick(View v) {
}
}
答案 0 :(得分:3)
您将为itemCount返回零,因此适配器认为您没有任何项目。在您的适配器中尝试一下:
new Date('2018-06-32')
Invalid Date