mardi 10 mars 2015

How to add films to a favorite list

I have made a film database app where the user can search for films, the search results then display in a ScrollView. The users can then click on the film results which leads onto another activity which displays the full film details. On this details page I also have a button, which I want the users to be able to click to add set that film as a favourite (which is currently set at a false boolean). The favourites will then be displayed in another activity. How would I go about doing this?


DbHelper:



public class DbHelper extends SQLiteOpenHelper {

private static final String ID = "id";
private static final String FILM = "film";
private static final String ACTOR = "actor";
private static final String ACTOR2 = "actor2";
private static final String DIRECTOR = "director";
private static final String DESCRIPTION = "description";
private static final String TABLE_NAME = "FILMTABLE";
private static final String DATABASE_NAME = "filmdatabase3";
private static final int DATABASE_VERSION = 1;
private static final boolean FAVOURITE = false;


public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FILM + " TEXT NOT NULL, " +
ACTOR + " TEXT NOT NULL, " +
ACTOR2 + " TEXT NOT NULL, " +
DIRECTOR + " TEXT NOT NULL, " +
FAVOURITE + " BOOLEAN, " +
DESCRIPTION + " TEXT NOT NULL);"
);

Cursor countRows = db.rawQuery("SELECT count(*) FROM FILMTABLE", null);
countRows.moveToFirst();
int NumRows = countRows.getInt(0);
countRows.close();

if (NumRows == 0) {
ContentValues values = new ContentValues();

values.put("film", "Wolf of Wall Street");
values.put("actor", "Leonardo Dicaprio");
values.put("actor2", "Jonah Hill");
values.put("director", "Martin Scorses");
values.put("description", "True story of New York stockbroker Jordan Belfort. From the American dream to corporate greed, Belfort goes from penny stocks and righteousness to IPOs and a life of corruption in the late 80's.");
db.insert("FILMTABLE", null, values);
values.clear();

values.put("film", "Captain Philips");
values.put("actor", "Tom Hanks");
values.put("actor2", "Catherine Keener");
values.put("director", "Paul Greengrass");
values.put("description", "True story, Captain Phillips is a multi-layered examination of the 2009 hijacking of the U.S. container ship Maersk Alabama by a crew of Samali pirates.");
db.insert("FILMTABLE", null, values);
values.clear();

}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
}


MainActivity:



public class MainActivity extends ActionBarActivity {
public final static String FILM_ID_KEY = "com.mnt.filmapp6.FILM_ID_KEY";
protected SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btnAddFilm = (Button) findViewById(R.id.add_film);
Button btnFavourite = (Button) findViewById(R.id.favourites);

btnAddFilm.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(intent);
}
});

btnFavourite.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity3.class);
startActivity(intent);
}
}
);
}

public void search(View v){
EditText search = (EditText)findViewById(R.id.edtSearch);
String searchresult = "%" + search.getText().toString() + "%";
db = new DbHelper(this).getReadableDatabase();
String[] column = {"*"};
String where = "film LIKE ? OR actor LIKE ? OR actor2 LIKE ? OR director LIKE ?";
String[] selArgs = {searchresult, searchresult, searchresult, searchresult};
Cursor results = db.query("FILMTABLE", column, where, selArgs, null, null, null);
film(results);
}

public void film (Cursor c){

c.moveToFirst();
int titleint = c.getColumnIndex("film");
int id= c.getColumnIndex("id");
String title = c.getString(titleint);
int filmID = c.getInt(id);

TextView txt = new TextView(getApplicationContext());
txt.setId(filmID);
txt.setText(title);
txt.setTextColor(Color.BLACK);
txt.setTextSize(16);
txt.setClickable(true);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity4.class);
intent.putExtra(FILM_ID_KEY, String.valueOf(v.getId()));
startActivity(intent);
}
});

ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
scrollView.addView(txt);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


MainActivity4:



public class MainActivity4 extends ActionBarActivity {

String filmId;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);

Intent intent = getIntent();
filmId= intent.getStringExtra(MainActivity.FILM_ID_KEY);

TextView txtFilm = (TextView) findViewById(R.id.txtFilm);
TextView txtActor = (TextView) findViewById(R.id.txtActor);
TextView txtActor2 = (TextView) findViewById(R.id.txtActor2);
TextView txtDirector = (TextView) findViewById(R.id.txtDirector);
TextView txtDescription = (TextView) findViewById(R.id.txtDescription);

db = new DbHelper(this).getReadableDatabase();
String[] column = {"*"};
String where = "id=?";
String[] selArgs = {filmId};
Cursor film = db.query("FILMTABLE", column, where, selArgs, null, null, null);

film.moveToFirst();
txtFilm.setText(film.getString(film.getColumnIndex("film")));
txtActor.setText(film.getString(film.getColumnIndex("actor")));
txtActor2.setText(film.getString(film.getColumnIndex("actor2")));
txtDirector.setText(film.getString(film.getColumnIndex("director")));
txtDescription.setText(film.getString(film.getColumnIndex("description")));
db.close();
}
}

Aucun commentaire:

Enregistrer un commentaire