RecyclerView没有显示任何项目 [英] RecyclerView is not showing any item

查看:72
本文介绍了RecyclerView没有显示任何项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,我从json响应中填充了哪些项目.

I have a RecyclerView which items I populate from json response.

public class UserProfile extends AppCompatActivity {

private UserData userData;
private ProfileNewsFeedAdapter adapter;
private ArrayList<ProgramModel> list;
private RecyclerView profileNewsFeed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_profile_activity);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    userData = new UserData(this);
    list = new ArrayList<>();
    adapter = new ProfileNewsFeedAdapter(getActivity(), list);

    initializeViews();
}

private void initializeViews() {

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setTitle(userData.getName());

    TextView userEmail = (TextView) findViewById(R.id.emailPrfId);
    TextView birthday = (TextView) findViewById(R.id.birthdayId);
    TextView userClass = (TextView) findViewById(R.id.userClassId);

    userEmail.setText(userData.getEmail());
    birthday.setText("Ditëlindja: " + userData.getBirthday());
    userClass.setText("Klasa: " + userData.getUserClass());
    setFont(userEmail);
    setFont(birthday);
    setFont(userClass);

    profileNewsFeed = (RecyclerView) findViewById(R.id.profileNewsFeed);
    profileNewsFeed.setLayoutManager(new LinearLayoutManager(this));
    profileNewsFeed.setItemAnimator(new DefaultItemAnimator());
    profileNewsFeed.setHasFixedSize(true);
    profileNewsFeed.setAdapter(adapter);


    GetBorrowedBooks getBorrowedBooks = new GetBorrowedBooks();
    getBorrowedBooks.execute();
}

private void setFont(TextView textView) {
    textView.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf"));
}

private AppCompatActivity getActivity() {
    return this;
}


private class GetBorrowedBooks extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... voids) {
        String userId = userData.getUserId();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://ec2-52-39-232-168.us-west-2.compute.amazonaws.com/user/" + userId + "/requests", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray(response);

                    if (jsonArray.length() > 0) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String imageName = "http://ec2-52-39-232-168.us-west-2.compute.amazonaws.com/files/books/" + jsonObject.getString("cover");

                            list.add(new ProgramModel(jsonObject.getString("title"), jsonObject.getString("author"), imageName));
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                MyDynamicToast.errorMessage(AppController.getInstance(), "Volley did not respond!");
            }
        });

        AppController.getInstance().addToRequestQueue(stringRequest);

        return null;
    }
}

}

这是适配器代码:

public class ProfileNewsFeedAdapter extends RecyclerView.Adapter<ProfileNewsFeedAdapter.MyViewHolder> {

private AppCompatActivity activity;
private ArrayList<ProgramModel> program;
private LayoutInflater inflater;

public ProfileNewsFeedAdapter(AppCompatActivity activity, ArrayList<ProgramModel> program) {
    this.activity = activity;
    inflater = activity.getLayoutInflater();
    this.program = program;
}

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView title, author;
    ImageView bookImage;

    MyViewHolder(View v) {
        super(v);
        title = (TextView) v.findViewById(R.id.bookTitleBorrowId);
        author = (TextView) v.findViewById(R.id.bookAuthorBorrowId);
        bookImage = (ImageView) v.findViewById(R.id.bookImageBorrowCardId);
    }

    void setMenuDetail(ProgramModel model, final int position) {
        title.setText(model.getTitle());
        author.setText(model.getMessage());

        // Set text fonts
        title.setTypeface(Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Bold.ttf"));
        author.setTypeface(Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Light.ttf"));

        Picasso.with(activity).load(model.getImageUrl()).into(bookImage);

        bookImage.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bookImageCardId:
                new GetBookInfo(activity, title.getText().toString()).execute();
                break;
        }
    }
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.profile_cardview_item, parent, false);
    return new MyViewHolder(v);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    ProgramModel menuModel = program.get(position);
    holder.setMenuDetail(menuModel, position);
}

@Override
public int getItemCount() {
    return program.size();
}

private class GetBookInfo extends AsyncTask<Void, Void, Void> {

    private String bookTitle, bookAuthor, bookDescription, requestedBook, bookUrl, bookId;
    private int copies;
    private Context c;

    GetBookInfo(Context c, String requestedBook) {
        this.c = c;
        this.requestedBook = requestedBook;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Void doInBackground(Void... voids) {
        fetchBookInfo();
        return null;
    }

    private void fetchBookInfo() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_FETCH_BOOKS, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        if (jsonObject.getString("title").equals(requestedBook)) {
                            bookId = "" + jsonObject.getInt("id");
                            bookTitle = jsonObject.getString("title");
                            bookAuthor = jsonObject.getString("author");
                            bookDescription = jsonObject.getString("description");
                            copies = jsonObject.getInt("quantity");
                            setBookUrl("http://ec2-52-39-232-168.us-west-2.compute.amazonaws.com/files/books/" + jsonObject.getString("cover"));
                            Intent intent = new Intent(c, BookActivity.class);
                            intent.putExtra("title", getBookTitle());
                            intent.putExtra("author", getBookAuthor());
                            intent.putExtra("bookId", getBookId());
                            intent.putExtra("description", getBookDescription());
                            intent.putExtra("copies", getCopies());
                            intent.putExtra("description", getBookDescription());
                            intent.putExtra("bookUrl", getBookUrl());
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            c.startActivity(intent);
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.e("JSON Error: ", "" + e.getMessage());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                MyDynamicToast.errorMessage(AppController.getInstance(), "Volley Error!");
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(stringRequest);
    }

    String getBookTitle() {
        return bookTitle;
    }

    String getBookAuthor() {
        return bookAuthor;
    }

    String getBookDescription() {
        return bookDescription;
    }

    String getBookId() {
        return bookId;
    }

    int getCopies() {
        return copies;
    }

    private void setBookUrl(String url) {
        bookUrl = url;
    }

    String getBookUrl() {
        return bookUrl;
    }

}

}

这是我从中获取项目数据的ProgramModel类:

This is the ProgramModel class from which I get item's datas:

public class ProgramModel {

private String title;
private String message;
private int image;
private String imageUrl;

public ProgramModel(String title, String message, int image) {
    this.title = title;
    this.message = message;
    this.image = image;
}

public ProgramModel(String title, String message) {
    this.title = title;
    this.message = message;

}

public ProgramModel(String title, String message, String imageUrl) {
    this.title = title;
    this.message = message;
    this.imageUrl = imageUrl;

}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getMessage() {
    return message;
}

int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}

public String getImageUrl() {
    return this.imageUrl;
}
}

这是主要的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:background="@color/white"
    android:fitsSystemWindows="true"
    tools:context="com.libraryhf.libraryharryfultz.activity.UserProfile">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="4">

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/userImageId"
                android:layout_width="0dp"
                android:layout_height="80dp"
                android:layout_marginTop="3dp"
                android:layout_weight="1"
                android:src="@drawable/prf_image" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp"
                android:layout_weight="3"
                android:orientation="vertical"
                android:padding="7dp">

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/emailPrfId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/birthdayId"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/selectableItemBackground"
                    android:clickable="true"
                    android:paddingTop="7dp" />

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/userClassId"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/selectableItemBackground"
                    android:clickable="true"
                    android:paddingTop="7dp" />

            </LinearLayout>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/profileNewsFeed"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="150dp"
        android:scrollbars="none" />

    </android.support.design.widget.CoordinatorLayout>

这是单个项目的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/item_cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        card_view:cardBackgroundColor="@color/colorAccent"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="5dp"
        card_view:contentPadding="7dp">

        <android.support.v7.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/bookTitleBorrowId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:layout_margin="5dp"
                android:text="@string/dummyText"
                android:textColor="@color/white"
                android:textSize="20sp" />


            <android.support.v7.widget.LinearLayoutCompat
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_marginStart="9dp"
                android:layout_weight="7"
                android:orientation="horizontal">

                <android.support.v7.widget.AppCompatImageView
                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_marginTop="1dp"
                    android:layout_weight="1"
                    app:srcCompat="@drawable/writer_icon" />

                <TextView
                    android:id="@+id/bookAuthorBorrowId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_weight="6"
                    android:text="@string/dummyText"
                    android:textColor="@color/white"
                    android:textSize="13sp" />

            </android.support.v7.widget.LinearLayoutCompat>


            <ImageView
                android:id="@+id/bookImageBorrowCardId"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"
                tools:ignore="ContentDescription" />

        </android.support.v7.widget.LinearLayoutCompat>




     </android.support.v7.widget.CardView>
    </LinearLayout>

很不幸,我的项目没有显示在屏幕上.我知道显示了recyclerview,因为我可以注意到顶部和底部的动画,但是其中没有这些项目.我在哪里错了?谢谢.

Unfortunately my items are not showing on the screen. I know that recyclerview is shown because I can notice the animations on top and on bottom but the items are not there. Where have I gone wrong here? Thank you.

推荐答案

在将新元素添加到列表后,尝试添加adapter.notifyDataSetChanged();.如果您不通知适配器列表已更改,它将不会更新recyclerview,因此数据始终是在开始时声明的空列表.

Try adding adapter.notifyDataSetChanged(); after you add new element to the list. If you don't notify the adapter that the list changed, it won't update the recyclerview so that the data is always the empty list that is declared at the start.

这篇关于RecyclerView没有显示任何项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆