0

好的,所以我的 DataBaseHandler 中有下一个代码

    // Getting single contact
    Database getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_MOVIES, new String[] { KEY_ID,
    ,KEY_SUM,KEY_ACT,KEY_TRAILER,KEY_PREVIEW,KEY_DIR,KEY_WRI }, KEY_ID + "=?",
    new String[] { String.valueOf(id) }, null, null, null);
    if (cursor != null)
    cursor.moveToFirst();

    Database contact = new Database(Integer.parseInt(cursor.getString(0)),
    cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),
cursor.getString(6),cursor.getString(7));

return contact;
    }

info.java 类

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);
        DatabaseHandler db = new DatabaseHandler(this);
                // for this example i want to read out ID 15 in my database
        db.getContact(15);

    }

数据库.java 类

// getting movie_director
            public String getMD(){
                return this._movie_director;
            }

            // setting movie_director
            public void setMD(String movie_director){   
                this._movie_director = movie_director;
            }

包含 textView 的 info.xml 文件

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

在这种情况下,我想在 SQLite 数据库的 textview 中显示电影导演,但我被困在db.getContact(15);(参见 info.java 类)之后。我可以调用数据库,但我不知道如何从我的 DatabaseHandler 中检索信息并将其放入 Textview。我试图这样做db.getMD();,但以这种方式检索它是行不通的。有任何想法吗?

4

1 回答 1

2

Your getContact() function is returning a Database Object, but you are calling the method getMD() to your DatabaseHandler.

I think first of all you should rename the class Database to something like Contact and return that in your getContact() function. After that store the returned value in a variable and call the function getMD(). The code can be something like this:

  DatabaseHandler db = new DatabaseHandler(this);
  Contact contact = db.getContact(15); //This is your what you call Database object

  //get View element
  TextView contactTextView = (TextView) findViewById(R.id.textView2);
  //Set contact information in the TextView
  contactTextView.setText(contact.getMD());
于 2013-05-21T00:01:52.117 回答