如何根据用户点击检索数据库值

时间:2015-03-06 11:03:51

标签: java android sql database android-studio

我制作了一个数据库电影应用程序,允许用户搜索电影。然后,结果将显示在ScrollView中。一旦返回搜索结果,我希望用户能够点击一部电影,这将导致一个新的活动,其中包含电影的全部细节。我该怎么做呢?

点击将导致一项活动,但我需要根据点击的电影显示不同的详细信息。我的数据库总共有20部电影。

MainActivity上的搜索功能:

    public void search(View v){
    EditText search = (EditText)findViewById(R.id.edtSearch);
    String searchresult = "%" + search.getText().toString() + "%";
    db = new DbHelper(this).getReadableDatabase();
    String[] tblColumns = {"*"};
    String where = "film LIKE ? OR actor LIKE ? OR actor2 LIKE ? OR director LIKE ?";
    String[] args = {searchresult, searchresult, searchresult, searchresult};
    Cursor results = db.query("FILMTABLE", tblColumns, where, args, 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(15);
    txt.setClickable(true);
    txt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), MainActivity4.class);
            startActivity(intent);
        }
    });

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

DatabaseHelper:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

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", "Gone Girl");
            values.put("actor", "Ben Affleck");
            values.put("actor2", "Rosamund Pike");
            values.put("director", "David Fincher");
            values.put("description", "description");
            db.insert("FILMTABLE", null, values);
            values.clear();

            values.put("film", "The Hobbit");
            values.put("actor", "Martin Freeman");
            values.put("actor2", "Elijah Wood");
            values.put("director", "Peter Jackson");
            values.put("description", "description");
            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);
    }
}

1 个答案:

答案 0 :(得分:0)

当您开始详细活动时,将电影的ID添加到您正在创建的Intent中。

Intent intent = new Intent(getApplicationContext(), MainActivity4.class);
intent.putExtra("FILM_ID_KEY", getSelectedFilmId());
startActivity(intent);

或者制作您的电影POJO parcelable并再次通过putExtra方法将其置于您的意图中。

之后,在您的MainActivity4中使用getIntent方法在onCreate中获取这些值。