簡體   English   中英

RecyclerView沒有顯示任何項目

[英]RecyclerView is not showing any item

我有一個RecyclerView我從json響應中填充哪些項目。

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類:

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,因為我可以注意到頂部和底部的動畫,但是其中沒有這些項目。 我在哪里錯了? 謝謝。

您從doInBackground訪問視圖時,在另一個線程上調用doInBackground。 您應該從onPostExecute()(在ui線程上調用的AsyncTask方法)訪問這些視圖。

嘗試添加adapter.notifyDataSetChanged(); 將新元素添加到列表后。 如果您不通知適配器列表已更改,則它不會更新recyclerview,因此數據始終是在開始時聲明的空列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM