【问题标题】:I need to show some data that I saved in my room database. What did I forget to add to code or what is it missing?我需要显示一些我保存在房间数据库中的数据。我忘记添加到代码中或缺少什么?
【发布时间】:2019-10-04 16:20:31
【问题描述】:

所以在这个项目中,我将电影的“评分”保存到我的房间数据库中。我明白了,我的电影名称和电影评分存储在数据库中,但是我只是不知道如何访问数据以在显示所有评分的片段中显示它。

这是我到目前为止的代码。

RatingActorAdapter.java

public class RatingMovieAdapter extends RecyclerView.Adapter<RatingMovieAdapter.ViewHolder> {
private List<Movie> movieList;
private LayoutInflater mInflater;

public RatingMovieAdapter(Context context, List<Movie> movieList) {
    this.movieList = movieList;
    this.mInflater = LayoutInflater.from(context);
}

@Override
public RatingMovieAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.list_movies_rating, parent, false);
    ViewHolder holder = new ViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

}

@Override
public void onBindViewHolder(RatingMovieAdapter.ViewHolder holder, int position, List<Object> payloads) {

    super.onBindViewHolder(holder,position, payloads);

    Movie item = this.movieList.get(position);
    holder.movieName.setText(item.getMovieName());
    holder.movieRate.setText(item.getRatingMovie());
}

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

public class ViewHolder extends RecyclerView.ViewHolder{
    TextView movieRate;
    TextView movieName;
    public ViewHolder(View itemView) {
        super(itemView);
        movieRate =  itemView.findViewById(R.id.ratingFilmGrade);
        movieName =  itemView.findViewById(R.id.ratingFilmText);
    }
}

RatingFragment.java

public class RatingFragment extends Fragment {


private RecyclerView recyclerViewMovies;
List<Movie> items;
//private RecyclerView.LayoutManager layoutManager;
private RatingMovieAdapter ratingMovieAdapter;

public RatingFragment(){

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater,container,savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_rating, container, false);

    MovieDao movieDao = db.movieDao();       
    List<Movie> movieList = movieDao.getMovies();

    this.recyclerViewMovies = (RecyclerView) view.findViewById(R.id.movieRecyclerView);
    this.ratingMovieAdapter = new RatingMovieAdapter(getActivity(), this.items);
    this.recyclerViewMovies.setAdapter(this.ratingMovieAdapter);
    this.recyclerViewMovies.setLayoutManager(new LinearLayoutManager(getActivity()));

    View rootView = inflater.inflate(R.layout.fragment_rating, container, false);
    return rootView;

}

AppDatabase.java

@Database(entities = {Movie.class}, version =4, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
    public abstract MovieDao movieDao();
}

在主要活动中,我可以全局访问数据库:

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    public static AppDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = Room.databaseBuilder(this, AppDatabase.class, "top_actor_baza")
                .allowMainThreadQueries()
                .fallbackToDestructiveMigration()
                .build()

    }
....
...
...
...
}

MovieDao.java

public interface MovieDao {

    @Query("SELECT * FROM Movie")
    List<Movie> getMovies();

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(Movie movies);

    @Update
    void update(Movie movies);
}

电影.java

@Entity
public class Movie {

//    @PrimaryKey(autoGenerate = true)
//    public int uid;

    @PrimaryKey @NonNull
    public String movieName;

    @ColumnInfo(name = "rating_movie")
    public String ratingMovie;

    @ColumnInfo(name = "year")
    public String year;

    /*public Movie(int uid, String movieName, String ratingMovie, String year) {
        this.uid = uid;
        this.movieName = movieName;
        this.ratingMovie = ratingMovie;
        this.year = year;

    }*/

//    public int getUid() {
//        return uid;
//    }
//
//    public void setUid(int uid) {
//        this.uid = uid;
//    }

    public String getMovieName() {
        return movieName;
    }

    public void setMovieName(String movieName) {
        this.movieName = movieName;
    }

    public String getRatingMovie() {
        return ratingMovie;
    }

    public void setRatingMovie(String ratingMovie) {
        this.ratingMovie = ratingMovie;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
}

fragment_rating.xml

<ScrollView 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:id="@+id/scrollView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:gravity="center"
            android:text="Top rated movies"
            android:textSize="24sp"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/actorRecyclerView" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/movieRecyclerView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:scrollbars="vertical"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.501"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:gravity="center"
            android:text="Top rated actors"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/actorRecyclerView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:scrollbars="vertical"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView" />
    </android.support.constraint.ConstraintLayout>

</ScrollView>

list_movies_rating.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="wrap_content"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/ratingFilmGrade"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </TextView>

    <TextView
        android:id="@+id/ratingFilmText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

现在我认为这就是您需要的几乎所有类和布局来帮助我。如果您需要的话,我还可以添加显示我如何将数据保存到数据库的类,但我认为到那时一切都很好。

如果可以,请提供帮助。谢谢。

【问题讨论】:

    标签: java android sqlite android-recyclerview android-room


    【解决方案1】:

    Android Room 被设计为 Android 架构 组件的一部分。从片段访问数据库是个坏主意。

    使用 MVVM 模式并创建一个带有 LiveData &lt;List&lt;Movie&gt;&gt; 类型字段的 RatingViewModel 类,它将接收来自类 MovieRepository 的数据并将您的 recyclerview 订阅到此

    这种模式的例子太多了,我喜欢this one from Google codelab

    你也可以在my github todo-app project上看到如何做到这一点

    【讨论】:

    • 感谢您的回答,尽管我对此很陌生,但我会尽力使其工作。我会尝试查找您的 github 项目..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多