[英]RecyclerView doesn't show any items
我希望通过使用Adapter
和RecyclerView
从天气报告中创建一堆可滚动的项目。 我的问题是,即使我用来提供数据的ArrayList<>
确实具有data ,该视图也不显示任何内容-而且没有错误,也没有崩溃。 该应用程序启动,没有任何显示。
我的ActivityMain.onCreate()
:
private JSONParser_basic parser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parser = new JSONParser_basic(getResources()
.getString(R.string.weather_json));
parser.divideToListElements();
NewsRecycleAdapter adapter = new NewsRecycleAdapter(parser.getListElements());
LinearLayoutManager lm = new LinearLayoutManager(this);
lm.setOrientation(LinearLayoutManager.VERTICAL);
RecyclerView rv = (RecyclerView) findViewById(R.id.recycleView);
rv.setLayoutManager(lm);
rv.setAdapter(adapter);
}
请注意, JSONParser_basic
是一个自制的简单类(实际上,要从特定字符串获取数据)。 我用它从JSON字符串中获取数据并将其加载到我的collection listElements
。 它完成了工作,我得到了数据,这不是问题。
我的News
课:
public class News {
private String max;
private String min;
private String weather;
private String windForce;
public News(String max, String min, String weather, String windForce) {
this.max = max;
this.min = min;
this.weather = weather;
this.windForce = windForce;
}
public String formReport(){
return max + "/" + min + ", "
+ weather + ", " +
"Wind=" + windForce;
}
}
我上载了此类以显示formReport
方法。
我的适配器:
public class NewsRecycleAdapter extends RecyclerView.Adapter<NewsRecycleAdapter.NewsViewHolder> {
List<News> items;
public NewsRecycleAdapter(List<News> items) {
this.items = items;
}
@Override
public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
return new NewsViewHolder(View.inflate(parent.getContext(),
R.layout.list_element,
null));
}
@Override
public void onBindViewHolder(NewsViewHolder holder, int position) {
News myNews = items.get(position);
holder.titleTV.setText(myNews.formReport());
}
@Override
public int getItemCount() {
return items == null ? 0 : items.size();
}
public class NewsViewHolder extends RecyclerView.ViewHolder {
public TextView titleTV, leadTV;
public NewsViewHolder(View itemView) {
super(itemView);
titleTV = (TextView) itemView.findViewById(R.id.title);
}
}
}
list_element.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="7dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="some gibberish for now"
android:textStyle="bold" />
最后是我的main_activity.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="com.smth.myname.jeeesoooon.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
我的应用程序的build.gradle
确实有这行:
compile 'com.android.support:recyclerview-v7:26.0.+'
为什么RecyclerView
高度和宽度为0dp
?
将其更改为以下内容:
android:layout_width="match_parent"
android:layout_height="wrap_content"
另外,在list_element.xml
,将布局高度更改为wrap_content
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.