Итак, в этом проекте я сохраняю «рейтинги» фильмов в моей базе данных. Я дошел до того, что название моего фильма и его скорость сохраняются в базе данных, но тогда я просто не знаю, как мне получить доступ к данным, чтобы показать их в моем фрагменте, где отображаются все рейтинги.
Вот мой код до сих пор.
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);
}
Movie.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;
}
}
фрагмент_рейтинга. 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>
Теперь я думаю, что это почти все классы и макеты, которые вам нужны, чтобы помочь мне. Я мог бы также добавить класс, где он показывает, как я сохраняю свои данные в базу данных, если вам это нужно, но я думаю, что с этим все в порядке.
Пожалуйста, помогите, если можете. Спасибо.