У меня есть проект для приложения android, мое приложение связано с фильмами, я использовал счетчик, чтобы выбрать название mov ie, и для каждого mov ie есть кнопка трейлера. мой вопрос, как я могу изменить значение кнопки в зависимости от названия mov ie. Я надеюсь, что мой вопрос был ясен
MainActivity класс
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
TextView director;
TextView year;
TextView genre;
ImageView imageView;
TextView rating;
Button trailer;
TextView movieStory;
String[] movieNames = {"Test"};
Spinner movieSpinner;
movieDB MovieDB;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movieSpinner = (Spinner) findViewById(R.id.moviespinner);
trailer = (Button) findViewById(R.id.button);
MovieDB = new movieDB(getApplicationContext());
rating = (TextView) findViewById(R.id.rateText);
director = (TextView) findViewById(R.id.director);
year = (TextView) findViewById(R.id.year);
genre = (TextView) findViewById(R.id.genretext);
imageView = (ImageView) findViewById(R.id.imageView);
movieStory = (TextView) findViewById(R.id.movieDetails);
movieNames = MovieDB.getMovieNames();
ArrayAdapter<String> movieAdapter = new ArrayAdapter<String>(getBaseContext(), R.layout.list_movie, movieNames);
movieAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
movieSpinner.setAdapter(movieAdapter);
movieSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
final int position, long arg3) {
if (movieNames.length > position) {
director.setText(MovieDB.getDirector(movieNames[position]));
year.setText(MovieDB.getYear(movieNames[position]).toString());
genre.setText(MovieDB.getGenre(movieNames[position]));
imageView.setImageResource(MovieDB.getPhoto(movieNames[position]));
rating.setText(((Float) MovieDB.getRate(movieNames[position])).toString());
movieStory.setText(MovieDB.getStory(movieNames[position]));
// here is the trailer button
trailer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.youtube.com/watch?v=gZjQROMAh_s"));
try {
MainActivity.this.startActivity(webIntent);
} catch (ActivityNotFoundException ex) {
}
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
база данных
movieDB класс
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
public class movieDB {
public static final String KEY_ID = "_id";
public static final String KEY_MOVIE_NAME =
"movie_name";
public static final String KEY_DIRECTOR =
"director_name";
public static final String KEY_YEAR =
"year";
public static final String KEY_GENRE =
"genre";
public static final String KEY_PHOTO =
"pic";
public static final String KEY_RATE =
"rate";
public static final String KEY_STORY =
"story";
public static final String KEY_URL =
"url";
private Context context;
private ModuleDBOpenHelper moduleDBOpenHelper;
public movieDB(Context context) {
this.context = context;
moduleDBOpenHelper = new ModuleDBOpenHelper(context, ModuleDBOpenHelper.DATABASE_NAME, null,
ModuleDBOpenHelper.DATABASE_VERSION);
// populate the database with some data in case it is empty
if (getAll().length == 0) {
this.addRow("1917", "Todd Phillips", 2019 ,"Crime, Drama, Thriller" , R.mipmap.mov_1917_foreground, 8.4f , context.getString(R.string.mov1917story),"https://www.youtube.com/watch?v=gZjQROMAh_s");
this.addRow("Avatar", "James Cameron", 2009, "Action, Adventure, Fantasy, Sci-Fi" , R.mipmap.avatar_foreground , 7.8f , context.getString(R.string.avatarstory),"https://www.youtube.com/watch?v=5PSNL1qE6VY");
this.addRow("Joker", "Todd Phillips", 2019,"Crime, Drama, Thriller" , R.mipmap.joker_foreground , 8.6f, context.getString(R.string.jokerstory),"https://www.youtube.com/watch?v=zAGVQLHvwOY");
this.addRow("Once Upon a Time in Hollywood", "Vladislav Kozlov", 2019,"Drama" , R.mipmap.hollywood_foreground , 7.7f , context.getString(R.string.hollywoodstory),"https://www.youtube.com/watch?v=ELeMaP8EPAA");
this.addRow("The Irishman", "Martin Scorsese", 2019,"Biography, Crime, Drama" , R.mipmap.irish_foreground , 8.0f , context.getString(R.string.irishmanstory),"https://www.youtube.com/watch?v=RS3aHkkfuEI");
}
}
public void addRow(String movieName, String director, int year, String genre , int pic , float rate , String story , String url) {
// Create a new row of values to insert.
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put(KEY_MOVIE_NAME, movieName);
newValues.put(KEY_DIRECTOR, director);
newValues.put(KEY_YEAR, year);
newValues.put(KEY_GENRE, genre);
newValues.put(KEY_PHOTO, pic);
newValues.put(KEY_RATE, rate);
newValues.put(KEY_STORY, story);
newValues.put(KEY_URL, url);
// Insert the row into your table
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
db.insert(ModuleDBOpenHelper.DATABASE_TABLE, null, newValues);
}
public String[] getAll() {
ArrayList<String> outputArray = new ArrayList<String>();
String[] result_columns = new String[]{
KEY_MOVIE_NAME, KEY_DIRECTOR, KEY_YEAR , KEY_GENRE};
String movieName;
String directorName;
int year;
String genre;
String where = null;
String whereArgs[] = null;
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
boolean result = cursor.moveToFirst();
while (result) {
movieName = cursor.getString(cursor.getColumnIndex(KEY_MOVIE_NAME));
directorName = cursor.getString(cursor.getColumnIndex(KEY_DIRECTOR));
year = cursor.getInt(cursor.getColumnIndex(KEY_YEAR));
genre = cursor.getString(cursor.getColumnIndex(KEY_GENRE));
outputArray.add(movieName + " "+ directorName + year + " " + genre);
result = cursor.moveToNext();
}
return outputArray.toArray(new String[outputArray.size()]);
}
public String[] getMovieNames() {
ArrayList<String> outputArray = new ArrayList<String>();
String[] result_columns = new String[]{
KEY_MOVIE_NAME};
String movieName;
String where = null;
String whereArgs[] = null;
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
//
boolean result = cursor.moveToFirst();
while (result) {
movieName = cursor.getString(cursor.getColumnIndex(KEY_MOVIE_NAME));
outputArray.add(movieName);
result = cursor.moveToNext();
}
return outputArray.toArray(new String[outputArray.size()]);
}
public String geturl(String movieName) {
String[] result_columns = new String[]{
KEY_ID, KEY_URL};
String where = KEY_MOVIE_NAME + "= ?";
String whereArgs[] = {movieName};
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
if (cursor.moveToFirst()) {
int columnGenre = cursor.getColumnIndex(KEY_URL);
return cursor.getString(columnGenre);
} else return null;
}
public Integer getYear(String movieName) {
String[] result_columns = new String[]{
KEY_ID, KEY_YEAR};
String where = KEY_MOVIE_NAME + "= ?";
String whereArgs[] = {movieName};
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
if (cursor.moveToFirst()) {
int columnYear = cursor.getColumnIndex(KEY_YEAR);
return cursor.getInt(columnYear);
} else return 0;
}
public String getGenre(String movieName) {
String[] result_columns = new String[]{
KEY_ID, KEY_GENRE};
String where = KEY_MOVIE_NAME + "= ?";
String whereArgs[] = {movieName};
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
if (cursor.moveToFirst()) {
int columnGenre = cursor.getColumnIndex(KEY_GENRE);
return cursor.getString(columnGenre);
} else return null;
}
public String getStory(String movieName) {
String[] result_columns = new String[]{
KEY_ID, KEY_STORY};
String where = KEY_MOVIE_NAME + "= ?";
String whereArgs[] = {movieName};
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
if (cursor.moveToFirst()) {
int columnStory = cursor.getColumnIndex(KEY_STORY);
return cursor.getString(columnStory);
} else return null;
}
public Integer getPhoto(String movieName) {
String[] result_columns = new String[]{
KEY_ID, KEY_PHOTO};
String where = KEY_MOVIE_NAME + "= ?";
String whereArgs[] = {movieName};
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
if (cursor.moveToFirst()) {
int columnPhoto = cursor.getColumnIndex(KEY_PHOTO);
return cursor.getInt(columnPhoto);
} else return 0;
}
public float getRate(String movieName) {
String[] result_columns = new String[]{
KEY_ID, KEY_RATE};
String where = KEY_MOVIE_NAME + "= ?";
String whereArgs[] = {movieName};
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
if (cursor.moveToFirst()) {
int columnRate = cursor.getColumnIndex(KEY_RATE);
return cursor.getFloat(columnRate);
} else return 0;
}
public String getDirector(String movieName) {
String[] result_columns = new String[]{
KEY_ID, KEY_DIRECTOR};
String where = KEY_MOVIE_NAME + "= ?";
String whereArgs[] = {movieName};
String groupBy = null;
String having = null;
String order = null;
SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
result_columns, where,
whereArgs, groupBy, having, order);
if (cursor.moveToFirst()) {
int columnDirector = cursor.getColumnIndex(KEY_DIRECTOR);
return cursor.getString(columnDirector);
} else return null;
}
private static class ModuleDBOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myDatabase.db";
private static final String DATABASE_TABLE = "Movies";
private static final int DATABASE_VERSION = 1;
// SQL Statement to create a new database.
private static final String DATABASE_CREATE = "create table " +
DATABASE_TABLE + " (" + KEY_ID +
" integer primary key autoincrement, " +
KEY_MOVIE_NAME + " text not null, " +
KEY_DIRECTOR + " text , " +
KEY_YEAR + " int , " +
KEY_GENRE + " text , " +
KEY_PHOTO + " int , " +
KEY_RATE + " float , " +
KEY_STORY + " text , " +
KEY_URL + " text );";
public ModuleDBOpenHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
// Called when there is a database version mismatch meaning that
// the version of the database on disk needs to be upgraded to
// the current version.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +
oldVersion + " to " +
newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new
// version. Multiple previous versions can be handled by
// comparing oldVersion and newVersion values.
// The simplest case is to drop the old table and create a new one.
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Create a new one.
onCreate(db);
}
}
}
XML Макет Activity_main. xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/moviespinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<RelativeLayout
android:id="@+id/directorcontainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/moviespinner">
<TextView
android:id="@+id/directorHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/director"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="@+id/director"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/directorHeading"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/yearcontainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/directorcontainer"
android:layout_toRightOf="@+id/directorcontainer"
android:layout_marginLeft="20dp"
android:layout_toEndOf="@+id/directorcontainer"
android:layout_marginStart="20dp">
<TextView
android:id="@+id/yearHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/year"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/yearHeading"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/genrecontainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/directorcontainer"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/yearcontainer"
android:layout_marginStart="20dp"
android:layout_toEndOf="@id/yearcontainer">
<TextView
android:id="@+id/genreHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/genre"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/genretext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/genreHeading"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<LinearLayout
android:layout_below="@+id/genrecontainer"
android:layout_width="match_parent"
android:layout_height="661dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="0dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="147dp"
android:contentDescription="@string/thumb"
app:srcCompat="@drawable/ic_launcher_background" />
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<Button
android:id="@+id/button"
style="@android:style/Widget.Button.Inset"
android:layout_width="98dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/trailer" />
<Space
android:layout_width="34dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="34dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/star"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
app:srcCompat="@android:drawable/btn_star_big_on" />
<TextView
android:id="@+id/rateText"
android:layout_width="35dp"
android:layout_height="match_parent"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/rate10"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="@string/_10" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="13dp" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/movieDetails"
android:layout_width="match_parent"
android:layout_height="180dp" />
<Space
android:layout_width="match_parent"
android:layout_height="50dp" />
<TextView
android:id="@+id/test_URL"
android:layout_width="match_parent"
android:layout_height="90dp" />
</LinearLayout>
</RelativeLayout>
XML Макет list_mov ie. xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
[Image] внешний вид приложения
Заранее спасибо