我希望使用Adapter
和RecyclerView
从天气预报中创建一组可滚动的项目。我的问题是,即使用于提供数据的ArrayList<>
确实有数据,视图也不显示任何内容 - 并且没有错误,也没有崩溃。该应用程序启动,没有任何显示。
我的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字符串中获取数据并将其加载到我的集合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.+'
答案 0 :(得分:2)
为什么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"