如何在单独的textViews中显示数据库字段

时间:2015-03-06 13:06:50

标签: java android sql database android-studio

我创建了一个数据库电影应用,用户可以在其中搜索电影,搜索结果会以滚动视图显示。然后,用户可以点击电影结果,这将导致另一个活动,该活动将显示完整的电影细节(电影,演员,导演)。我如何检索数据库字段并在单独的textViews中显示这些字段?

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);
            intent.putExtra("FILM_ID_KEY", String.valueOf(v.getId()));
            startActivity(intent);
        }
    });

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

MainActivity4:

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

public class MainActivity4 extends ActionBarActivity {

    String filmId;
    protected 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);
    }
}

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", "Description");
            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", "Description");
            db.insert("FILMTABLE", null, values);
            values.clear();

       }
    }

1 个答案:

答案 0 :(得分:0)

这个问题的答案在于使用ListViews(https://developer.android.com/reference/android/widget/ListView.html)。基本上你应该创建一个ArrayAdapter,它接收你从数据库中检索的FilmObjects列表。在此适配器中,您将定义如何为每行显示此数据。将适配器附加到listview时,Android将负责每行的可视化。

本教程肯定会有所帮助:http://developer.android.com/guide/topics/ui/layout/listview.html

我建议摆脱构成电影的独立字符串,而是创建一个包含所有这些字段的类。这样可以更轻松地处理数据。