android中预先填充的数据库

时间:2014-07-05 20:18:58

标签: android android-sqlite

我需要这个应用程序的帮助。我想在Android应用程序中嵌入一个数据库。我的表有3列(_id,标题,文本)。应用程序的第一页显示列表视图中的标题,但是当您单击列表中的项目时,我想在另一个活动中显示该文本。到目前为止,这是怎么回事。 (我在网上发现了这个项目,并对其进行了修改。)

package com.softeq.prepopdb.activity;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.softeq.android.prepopdb.R;
import com.softeq.prepopdb.dbhelper.ExternalDbOpenHelper;

public class PrepopSqliteDbActivity extends ListActivity {
private static final String DB_NAME = "schymn.sqlite";
private static final String TABLE_NAME = "Hymns";
private static final String Hymn_ID = "_id";
private static final String Hymn_Title = "Title";
private static final String Hymn_Text = "Text";
    String lyrics;

private SQLiteDatabase database;
private ListView listView;
private ArrayList<String> Songs;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
    database = dbOpenHelper.openDataBase();

    fillHymns();
    setUpList();      
}

private void setUpList() {

    setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, Songs));
    listView = getListView();


    /*listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                            int position,long id) {
            Toast.makeText(getApplicationContext(),
                        ((TextView) view).getText().toString(),
                         Toast.LENGTH_SHORT).show();                
        }
    });*/

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                            int position,long id) {

        }
    });
}


private void fillHymns() {
    Songs = new ArrayList<String>();
    Cursor hymnCursor = database.query(TABLE_NAME,
                                         new String[] 
                                         {Hymn_ID, Hymn_Title,Hymn_Text},
                                         null, null, null, null
                                         , Hymn_Title);
    hymnCursor.moveToFirst();
    if(!hymnCursor.isAfterLast()) {
        do {
            String title = hymnCursor.getString(1);
            lyrics = hymnCursor.getString(2);
            Songs.add(title);
            //Songs.add(lyrics);
        } while (hymnCursor.moveToNext());
    }
    hymnCursor.close();
}
}

我试过这个,但它没有用。

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                            int position,long id) {
                        Log.i("text",lyrics);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

感谢大家,我能够通过创建适配器并将详细信息存储在键值对中来解决问题。