添加到会议室数据库

时间:2019-04-12 23:30:37

标签: java android database

我正在尝试将数据添加到数据库中,但不确定如何执行该方法。我已经创建了一个数据访问接口和一个插入方法。我可能是错的,但也不确定是否要插入正确的字段。这是我第一次使用房间。这是onSetClickListener的“ mFavorites”底部的addToFavourites方法。

*下面添加了电影课

public class DetailActivity extends AppCompatActivity {

    public static final String KEY_EXAMPLE = "hNCmb-4oXJA";
    private static final String KEY_ID = "id";
    private static final String KEY_URL = "key";
    private static final String KEY_NAME = "name";

    private RequestQueue mRequestQueue;
    private MovieDataBase database;
    ArrayList<TrailerRequest> mTrailerList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.movie_detail_actvity);
        ImageView imageView = findViewById(R.id.image_iv);


        mRequestQueue = Volley.newRequestQueue(this);

        mTrailerList = new ArrayList<>();


        /* database instance */
        database = MovieDataBase.getInstance(getApplicationContext());


        ImageView mFavourites = findViewById(R.id.fav_image_view);
        ImageView mPlayTrailer = findViewById(R.id.trailer_image_view);



        // String intents for catching the data (String constants) from Main activity for displaying data in the detail activity
        Intent intent = getIntent();
        String posterUrl = intent.getStringExtra(Constants.EXTRA_URL);
        String title = intent.getStringExtra(Constants.TITLE_TEXT);
        String overview = intent.getStringExtra(Constants.OVERVIEW_TEXT);
        String releaseDate = intent.getStringExtra(Constants.RELEASE);
        String voteAverage = intent.getStringExtra(Constants.VOTE_AVERAGE);
        final String movieId = intent.getStringExtra(MOVIE_ID);


        // Detail activity title TextView
        TextView textView = findViewById(R.id.title_text_view);
        // Detail activity OverView TextView
        TextView overViewText = findViewById(R.id.plot_synopsis_text_view);
        // Detail activity ReleaseDate TextView
        TextView releaseTextView = findViewById(R.id.release_date_text_view);
        // Detail activity user ratings TextView
        TextView voteAverageTextView = findViewById(R.id.user_rating_text_view);

        TextView movieIdTextView = findViewById(R.id.movie_id);


        // Load Detail activity ImageView using Picasso
        Picasso.get()
                .load(Utils.buildPosterUrl(posterUrl))
                .fit()
                .centerInside()
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.sample_7)
                .into(imageView);


        // set data on to views
        textView.setText(title);
        overViewText.setText(overview);
        releaseTextView.setText(releaseDate);
        voteAverageTextView.setText(String.format(getString(R.string.user_rating_tv), voteAverage));
        movieIdTextView.setText(movieId);


        mFavourites.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 addToFavourites(args????)//I don't know what to do here 
                Toast.makeText(DetailActivity.this, "Added to Favourites", Toast.LENGTH_SHORT).show();
            }
        });

        mPlayTrailer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playTrailer();
            }
        });

        mReadReviews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(DetailActivity.this, "Show Reviews", Toast.LENGTH_SHORT).show();
            }
        });


    }



    private void playTrailer() {

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + KEY_URL));
        startActivity(intent);

    }


    private void addToFavourites(final Movies movies) {
        AppExecutors.getInstance().diskIO().execute(new Runnable() {
            @Override
            public void run() {
                database.movieDao().insert(movies);
            }
        });
    }



    @Dao
    public interface MovieDao {
        @Insert(onConflict = OnConflictStrategy.REPLACE)
        void insert(example.android.popularmoviesvolley.Models.Movies[] movies);

        @Delete
        void delete(Movies[] movie);

        @Query("DELETE FROM movie_table")
        void deleteAllMovies();


        //This is where we get all movies displayed in the recycler view (COME BACK TO THIS!!)
        @Query("SELECT * FROM movie_table")
        LiveData<List<Movie>> getAllMovies();


    }

@Entity(tableName =“ movie_table”) 公共类Movies实现了Parcelable {

@PrimaryKey
private int id;

private String mId;

private String mOriginalTitle;

private String mPosterPath;

private String mOverview;

private String mReleaseDate;

private String mVoteAverage;


public Movies(String id, String posterPath, String originalTitle, String overview, String release_date, String voteAverage) {
    this.mId = id;
    this.mPosterPath = posterPath;
    this.mOriginalTitle = originalTitle;
    this.mOverview = overview;
    this.mReleaseDate = release_date;
    this.mVoteAverage = voteAverage;


}


@Ignore
public Movies(Parcel parcel) {
    mId = parcel.readString();
    mOriginalTitle = parcel.readString();
    mPosterPath = parcel.readString();
    mOverview = parcel.readString();
    mReleaseDate = parcel.readString();
    mVoteAverage = parcel.readString();
}

@Ignore
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mId);
    dest.writeString(mOriginalTitle);
    dest.writeString(mPosterPath);
    dest.writeString(mOverview);
    dest.writeString(mReleaseDate);
    dest.writeString(mVoteAverage);
}

public static final Creator<Movies> CREATOR = new Creator<Movies>() {
    @Override
    public Movies createFromParcel(Parcel in) {
        return new Movies(in);
    }

    @Override
    public Movies[] newArray(int size) {
        return new Movies[size];
    }
};

@Ignore
@Override
public int describeContents() {
    return 0;
}


public String getmOriginalTitle() {
    return mOriginalTitle;
}

public void setmOriginalTitle(String mOriginalTitle) {
    this.mOriginalTitle = mOriginalTitle;
}


public String getmOverview() {
    return mOverview;
}

public void setmOverview(String mOverview) {
    this.mOverview = mOverview;
}

public String getmReleaseDate() {
    return mReleaseDate;
}

public void setmReleaseDate(String mReleasedate) {
    this.mReleaseDate = mReleasedate;
}

public String getmPosterPath() {
    return mPosterPath;
}

public void setmPosterPath(String mPosterPath) {
    this.mPosterPath = mPosterPath;
}

public String getmVoteAverage() {
    return mVoteAverage;
}

public void setmVoteAverage(String mVoteAverage) {
    this.mVoteAverage = mVoteAverage;
}

public String getmId() {
    return mId;
}

public void setmId(String mId) {
    this.mId = mId;
}

//Database ID
public void setId(int id) {
    this.id = id;
}

1 个答案:

答案 0 :(得分:0)

  

addToFavourites(args ????)

看起来您的addToFavourites方法采用了Movies对象,因此请确保该对象使用@Entity注释并且具有@PrimaryKey(在所有电影中都是唯一的)。然后将其传递。

我还注意到您在DAO中有两个insert语句,它们接收一个Movie对象。我认为这是正确的:

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(example.android.popularmoviesvolley.Models.Movies[] movies);

所以删除另一个

然后在RoomDatabase类中,确保您具有以下内容:

  @Database(entities = {Movies.class}, version = 1, exportSchema = false)
  public abstract class MyDatabase extends RoomDatabase {
     ...
  }

如果这不能回答您的问题,请更新您的帖子并显示您的电影课。

修改

    Intent intent = getIntent();
    String posterUrl = intent.getStringExtra(Constants.EXTRA_URL);
    String title = intent.getStringExtra(Constants.TITLE_TEXT);
    String overview = intent.getStringExtra(Constants.OVERVIEW_TEXT);
    String releaseDate = intent.getStringExtra(Constants.RELEASE);
    String voteAverage = 
    intent.getStringExtra(Constants.VOTE_AVERAGE);
    final String movieId = intent.getStringExtra(MOVIE_ID);

    Movies movies = new Movies(movieId, posterUrl, title, overview, 
    releaseDate, voteAverage);

     mFavourites.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             addToFavourites(movies)//I don't know what to do here 
            Toast.makeText(DetailActivity.this, "Added to Favourites", 
            Toast.LENGTH_SHORT).show();
        }
    });