[英]Android | RecyclerView doesn't work
I want to create a RecyclerView
with a few CardView
s in it. 我想创建一个
RecyclerView
其中包含一些CardView
。 For this I start a Thread
in the onCreate
of my Activity.
为此,我在
Activity.
的onCreate
中启动一个Thread
Activity.
There I get the data from my server and put this in a list. 在那里,我从服务器获取数据并将其放在列表中。 Then I create the Adapter for the
RecyclerView,
but it doesn't work. 然后,我为
RecyclerView,
创建适配器RecyclerView,
但是它不起作用。 Here is my code: 这是我的代码:
Here I create the adapter and it should fill all CardView
s in: 在这里,我创建了适配器,它应该填充所有
CardView
:
ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setAdapter(adapter);
Here is my adapter class 这是我的适配器类
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PostViewHolder> {
List<Post> posts;
public ListAdapter(List<Post> posts) {
Log.d("ListAdapter", "");
this.posts = posts;
}
@Override
public int getItemCount() {
Log.d("getItemCount", "");
return posts.size();
}
@Override
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
Log.d("onCreateView", "");
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.insert_layout, viewGroup, false);
PostViewHolder pvh = new PostViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
Log.d("onBindView", "");
postViewHolder.username.setText(posts.get(i).getUsername());
postViewHolder.text.setText(posts.get(i).getText());
postViewHolder.time.setText(Long.toString(posts.get(i).getTime()));
postViewHolder.postPhoto.setImageResource(posts.get(i).returnIMG());
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d("onAttached", "");
super.onAttachedToRecyclerView(recyclerView);
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView username;
TextView time;
TextView text;
ImageView postPhoto;
PostViewHolder(View itemView) {
super(itemView);
Log.d("PostViewHolder", "");
cv = (CardView) itemView.findViewById(R.id.cv);
username = (TextView) itemView.findViewById(R.id.usernameText);
time = (TextView) itemView.findViewById(R.id.timeText);
text = (TextView) itemView.findViewById(R.id.textText);
postPhoto = (ImageView) itemView.findViewById(R.id.postPhoto);
}
}
Here is my recyclerview: 这是我的recyclerview:
<android.support.v7.widget.RecyclerView
android:id="@+id/post_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
and my cardview: 和我的Cardview:
<android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/postPhoto"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textText"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/usernameText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeText"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
And here the 2 errors: 这里是两个错误:
E/RecyclerView: No adapter attached;
E / RecyclerView:未连接适配器; skipping layout E/RecyclerView: No layout manager attached;
跳过布局E / RecyclerView:未连接布局管理器; skipping layout
跳过布局
But I do both, dont't I? 但是我都做,不是吗?
set the LayoutManager
first. 首先设置
LayoutManager
。
From your code : 从您的代码:
ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
rv.setAdapter(adapter);
Hope this will help you!!.. 希望这个能对您有所帮助!!..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.