简体   繁体   中英

RecyclerView doesn't show any items

I wish to create a scrollable bunch of items from weather reports by using an Adapter and a RecyclerView . My problem is that even though the ArrayList<> I use to provide data does have data , the view just doesn't show anything - and no errors, no crashes. The app starts and nothing to show.

My 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);

}

Note that the JSONParser_basic is a self-made simple class (out of practice, to get data from a specific string). I use it to get data from a JSON string and load it to my collection listElements . It does its job, I get the data, that isn't the issue.

My News class:

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;
    }
}

I uploaded this class to show the formReport method.

My adapter:

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);
        }
    }


}    

The 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" />

And finally my 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" />

And I do have this line in my app's build.gradle :

compile 'com.android.support:recyclerview-v7:26.0.+'

Why is the RecyclerView height and width 0dp ?

Change it to the following:

android:layout_width="match_parent"
android:layout_height="wrap_content"

Also, in list_element.xml , change the layout height to wrap_content :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM