ListView 上方的 SQLite- 标题(来源与 ListView 来源相同)
SQLite- heading above ListView (source same as ListView source)
我正忙于一个圣经应用程序,我使用资产文件夹中的 SQLite 数据库检索数据,我使用 3 个列表视图,每个列表视图都有自己的 activity,它是这样的:
DB 列名称: 书名、书 ID、章节编号、章节 ID、节文本、节 ID
第 1 activity,用户 select 是一本书,创世记、出埃及记、利未记等...第 2 activity 开始
第 2 activity,数据库被过滤,因此用户可以 select 那本书下的章节...和第 3 activity 开始
3rd activity,db 被过滤并显示该章节下的所有经文...
我想要的是在第二个列表视图的顶部放置一个文本视图,然后在用户从第一个列表视图 select 编辑后,第二个 activity 开始显示所有章节,它应该显示从第一本 activity 开始 select 编辑的书名。在第三个列表视图中,它应该显示从前两个活动中 select 编辑的书名和章节编号,我尝试使用 intend,但出现错误。
适配器:
public class customAdapterHoofstuk extends BaseAdapter {
private Context mContext;
private List<defineBybeldbAlles> defineBybeldbAlles;
public customAdapterHoofstuk(Context mContext, List<defineBybeldbAlles> defineBybelDBList) {
this.mContext = mContext;
this.defineBybeldbAlles = defineBybelDBList;
}
@Override
public int getCount() {
return defineBybeldbAlles.size();
}
@Override
public Object getItem(int position) {
return defineBybeldbAlles.get(position);
}
@Override
public long getItemId(int position) {
return (defineBybeldbAlles.get(position).getHoofstuk_id());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(mContext, R.layout.custom_row_hoofstuk, null);
//this works->
TextView hoofstuknommer = (TextView)v.findViewById(R.id.custom_row_hoofstuktext);
hoofstuknommer.setText (defineBybeldbAlles.get(position).getHoofstuk_nommer());
//this works-->
TextView hoofstukid = (TextView)v.findViewById(R.id.hoofstuk_id);
hoofstukid.setText(String.valueOf(defineBybeldbAlles.get(position).getHoofstuk_id()));
//this doesnt work->
TextView boeknaambyhoofstuk = (TextView)v.findViewById(R.id.boeknaambyhoofstuklys);
boeknaambyhoofstuk.setText(defineBybeldbAlles.get(position).get_hebreeus());
return v;
}
}
Activity 应显示的位置:
public class BybelActivityHoofstuk extends Activity {
private ListView listviewHoofstuk;
private customAdapterHoofstuk adapter_customAdapterHoofstuk;
private List<defineBybeldbAlles> defineBybeldbAllesList;
private DBHandlerHoofstuk DBHandlerHoofstuk;
ArrayList<HashMap<String, String>> HoofstukList;
//Boek id
String boek_id_na_hoofstuk;
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bybel_hoofstuk);
listviewHoofstuk = (ListView) findViewById(R.id.BybelHoofstukListView);
DBHandlerHoofstuk = new DBHandlerHoofstuk(this);
//Check exists database
File Database = getApplicationContext().getDatabasePath(DBHandlerHoofstuk.DBNAME);
if(false == Database.exists()){
DBHandlerHoofstuk.getReadableDatabase();}
//Get boek id
Intent boekIntent = getIntent();
boek_id_na_hoofstuk = boekIntent.getStringExtra("boek_id");
//hashmap for listview
HoofstukList = new ArrayList<HashMap<String, String>>();
//Get bybel list in db when db exists
defineBybeldbAllesList = DBHandlerHoofstuk.getListHoofstuk(boek_id_na_hoofstuk);
//Init adapter
adapter_customAdapterHoofstuk = new customAdapterHoofstuk(this,defineBybeldbAllesList);
//Set adapter for listview
listviewHoofstuk.setAdapter(adapter_customAdapterHoofstuk);
//Listview item click listener
//BybelActivityVers will be launched by passing hoofstuk_id
listviewHoofstuk.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){
//on selecting a hoofstk
//BybelActivityVers will be launched to show verse inside
Intent hoofstukIntent = new Intent(BybelActivityHoofstuk.this,BybelActivityVers.class);
//send hoofstuk_id to VersActivity to get verse under that book
String hoofstuk_id_na_vers = ((TextView)view.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukIntent.putExtra("hoofstuk_id", hoofstuk_id_na_vers);
startActivity(hoofstukIntent);
}
});
}
}
数据库处理程序:
public class DBHandlerHoofstuk extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DBNAME = "pwl14082016-5.db";
public static final String DBLOCATION = "location goes here";
private Context mContext;
private SQLiteDatabase mDatabase;
public static final String COLUMN_BOEK_ID = "boek_id";
public static final String COLUMN_HEBREEUS = "_hebreeus";
public static final String COLUMN_AFRIKAANS = "_afrikaans";
public static final String COLUMN_HOOFSTUK_ID = "hoofstuk_id";
public static final String COLUMN_HOOFSTUK_NOMMER = "hoofstuk_nommer";
public static final String COLUMN_VERS_ID = "vers_id";
public static final String COLUMN_VERS_NOMMER = "vers_nommer";
public static final String COLUMN_VERS_TEXT = "vers_text";
public DBHandlerHoofstuk(Context context) {
super(context, DBNAME, null, DATABASE_VERSION);
this.mContext = context;
}
//Blank want db bestaan klaar
@Override
public void onCreate(SQLiteDatabase db) {
}
//blank want db word ekstern geupgrade
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//maak db oop
public void opendatabase(){
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if (mDatabase !=null && mDatabase.isOpen()) {
return;
}
//verander dalk na 'mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);' as OPEN_READONLY nie werk nie
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
//maak db toe
public void closeDatabase(){
if (mDatabase!=null) {
mDatabase.close();
}
}
public List<defineBybeldbAlles> getListHoofstuk(String boek_id_na_hoofstuk){
defineBybeldbAlles defineBybeldbHoofstuk = null;
List<defineBybeldbAlles> defineBybeldbAllesList = new ArrayList<>();
opendatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM PWLBybel WHERE " + COLUMN_BOEK_ID + " = '" + boek_id_na_hoofstuk + "'GROUP BY hoofstuk_id ORDER BY hoofstuk_id * 1 ASC", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()){
defineBybeldbHoofstuk = new defineBybeldbAlles(cursor.getInt(0), cursor.getString(1),cursor.getString(2),cursor.getInt(3),cursor.getString(4),cursor.getInt(5),cursor.getString(6),cursor.getString(7));
defineBybeldbAllesList.add(defineBybeldbHoofstuk);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return defineBybeldbAllesList;
}
}
XML 显示位置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".defineBybeldbAlles">
<ListView
android:id="@+id/BybelHoofstukListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#ff303030"
android:dividerHeight="1dp"
android:layout_marginTop="21dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Boek naam:"
android:id="@+id/boeknaambyhoofstuklys"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#000063"
android:textSize="20dp" />
</RelativeLayout>
您可以使用
通过 intent 发送字符串
String KEY = "PUT_ANY_KEY_HERE";
String VALUE = "BOOK_NAME/CHAPTER_NAME";
Intent i = new Intent(FROM_CLASS.this, TO_CLASS.class);
i.putExtra(KEY,VALUE);
startActivity(i);
并使用
获取第二个activity中的字符串
Intent intent = getIntent();
if(intent!=null)
String VALUE = intent.getExtras().getString(KEY);
现在在文本视图中设置 VALUE 字符串。此外,您还需要将 chapterName 文本视图放在列表视图上方,或者将 VALUE 字符串添加为数组列表中的第一个条目,您可能会使用它来填充第二和第三个 activity 中的列表视图。
如果有人遇到这个问题,我无法将书籍和章节名称传递到标题或栏中,但我设法设置了一个标题。
在每个 activity 下的清单中,只需输入 android:label="title here"
至少得到一个你想要的标题。
我正忙于一个圣经应用程序,我使用资产文件夹中的 SQLite 数据库检索数据,我使用 3 个列表视图,每个列表视图都有自己的 activity,它是这样的:
DB 列名称: 书名、书 ID、章节编号、章节 ID、节文本、节 ID
第 1 activity,用户 select 是一本书,创世记、出埃及记、利未记等...第 2 activity 开始
第 2 activity,数据库被过滤,因此用户可以 select 那本书下的章节...和第 3 activity 开始
3rd activity,db 被过滤并显示该章节下的所有经文...
我想要的是在第二个列表视图的顶部放置一个文本视图,然后在用户从第一个列表视图 select 编辑后,第二个 activity 开始显示所有章节,它应该显示从第一本 activity 开始 select 编辑的书名。在第三个列表视图中,它应该显示从前两个活动中 select 编辑的书名和章节编号,我尝试使用 intend,但出现错误。
适配器:
public class customAdapterHoofstuk extends BaseAdapter {
private Context mContext;
private List<defineBybeldbAlles> defineBybeldbAlles;
public customAdapterHoofstuk(Context mContext, List<defineBybeldbAlles> defineBybelDBList) {
this.mContext = mContext;
this.defineBybeldbAlles = defineBybelDBList;
}
@Override
public int getCount() {
return defineBybeldbAlles.size();
}
@Override
public Object getItem(int position) {
return defineBybeldbAlles.get(position);
}
@Override
public long getItemId(int position) {
return (defineBybeldbAlles.get(position).getHoofstuk_id());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(mContext, R.layout.custom_row_hoofstuk, null);
//this works->
TextView hoofstuknommer = (TextView)v.findViewById(R.id.custom_row_hoofstuktext);
hoofstuknommer.setText (defineBybeldbAlles.get(position).getHoofstuk_nommer());
//this works-->
TextView hoofstukid = (TextView)v.findViewById(R.id.hoofstuk_id);
hoofstukid.setText(String.valueOf(defineBybeldbAlles.get(position).getHoofstuk_id()));
//this doesnt work->
TextView boeknaambyhoofstuk = (TextView)v.findViewById(R.id.boeknaambyhoofstuklys);
boeknaambyhoofstuk.setText(defineBybeldbAlles.get(position).get_hebreeus());
return v;
}
}
Activity 应显示的位置:
public class BybelActivityHoofstuk extends Activity {
private ListView listviewHoofstuk;
private customAdapterHoofstuk adapter_customAdapterHoofstuk;
private List<defineBybeldbAlles> defineBybeldbAllesList;
private DBHandlerHoofstuk DBHandlerHoofstuk;
ArrayList<HashMap<String, String>> HoofstukList;
//Boek id
String boek_id_na_hoofstuk;
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bybel_hoofstuk);
listviewHoofstuk = (ListView) findViewById(R.id.BybelHoofstukListView);
DBHandlerHoofstuk = new DBHandlerHoofstuk(this);
//Check exists database
File Database = getApplicationContext().getDatabasePath(DBHandlerHoofstuk.DBNAME);
if(false == Database.exists()){
DBHandlerHoofstuk.getReadableDatabase();}
//Get boek id
Intent boekIntent = getIntent();
boek_id_na_hoofstuk = boekIntent.getStringExtra("boek_id");
//hashmap for listview
HoofstukList = new ArrayList<HashMap<String, String>>();
//Get bybel list in db when db exists
defineBybeldbAllesList = DBHandlerHoofstuk.getListHoofstuk(boek_id_na_hoofstuk);
//Init adapter
adapter_customAdapterHoofstuk = new customAdapterHoofstuk(this,defineBybeldbAllesList);
//Set adapter for listview
listviewHoofstuk.setAdapter(adapter_customAdapterHoofstuk);
//Listview item click listener
//BybelActivityVers will be launched by passing hoofstuk_id
listviewHoofstuk.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick (AdapterView<?> arg0, View view, int arg2, long arg3){
//on selecting a hoofstk
//BybelActivityVers will be launched to show verse inside
Intent hoofstukIntent = new Intent(BybelActivityHoofstuk.this,BybelActivityVers.class);
//send hoofstuk_id to VersActivity to get verse under that book
String hoofstuk_id_na_vers = ((TextView)view.findViewById(R.id.hoofstuk_id)).getText().toString();
hoofstukIntent.putExtra("hoofstuk_id", hoofstuk_id_na_vers);
startActivity(hoofstukIntent);
}
});
}
}
数据库处理程序:
public class DBHandlerHoofstuk extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DBNAME = "pwl14082016-5.db";
public static final String DBLOCATION = "location goes here";
private Context mContext;
private SQLiteDatabase mDatabase;
public static final String COLUMN_BOEK_ID = "boek_id";
public static final String COLUMN_HEBREEUS = "_hebreeus";
public static final String COLUMN_AFRIKAANS = "_afrikaans";
public static final String COLUMN_HOOFSTUK_ID = "hoofstuk_id";
public static final String COLUMN_HOOFSTUK_NOMMER = "hoofstuk_nommer";
public static final String COLUMN_VERS_ID = "vers_id";
public static final String COLUMN_VERS_NOMMER = "vers_nommer";
public static final String COLUMN_VERS_TEXT = "vers_text";
public DBHandlerHoofstuk(Context context) {
super(context, DBNAME, null, DATABASE_VERSION);
this.mContext = context;
}
//Blank want db bestaan klaar
@Override
public void onCreate(SQLiteDatabase db) {
}
//blank want db word ekstern geupgrade
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
//maak db oop
public void opendatabase(){
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if (mDatabase !=null && mDatabase.isOpen()) {
return;
}
//verander dalk na 'mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);' as OPEN_READONLY nie werk nie
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
//maak db toe
public void closeDatabase(){
if (mDatabase!=null) {
mDatabase.close();
}
}
public List<defineBybeldbAlles> getListHoofstuk(String boek_id_na_hoofstuk){
defineBybeldbAlles defineBybeldbHoofstuk = null;
List<defineBybeldbAlles> defineBybeldbAllesList = new ArrayList<>();
opendatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM PWLBybel WHERE " + COLUMN_BOEK_ID + " = '" + boek_id_na_hoofstuk + "'GROUP BY hoofstuk_id ORDER BY hoofstuk_id * 1 ASC", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()){
defineBybeldbHoofstuk = new defineBybeldbAlles(cursor.getInt(0), cursor.getString(1),cursor.getString(2),cursor.getInt(3),cursor.getString(4),cursor.getInt(5),cursor.getString(6),cursor.getString(7));
defineBybeldbAllesList.add(defineBybeldbHoofstuk);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return defineBybeldbAllesList;
}
}
XML 显示位置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".defineBybeldbAlles">
<ListView
android:id="@+id/BybelHoofstukListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#ff303030"
android:dividerHeight="1dp"
android:layout_marginTop="21dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Boek naam:"
android:id="@+id/boeknaambyhoofstuklys"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#000063"
android:textSize="20dp" />
</RelativeLayout>
您可以使用
通过 intent 发送字符串String KEY = "PUT_ANY_KEY_HERE";
String VALUE = "BOOK_NAME/CHAPTER_NAME";
Intent i = new Intent(FROM_CLASS.this, TO_CLASS.class);
i.putExtra(KEY,VALUE);
startActivity(i);
并使用
获取第二个activity中的字符串Intent intent = getIntent();
if(intent!=null)
String VALUE = intent.getExtras().getString(KEY);
现在在文本视图中设置 VALUE 字符串。此外,您还需要将 chapterName 文本视图放在列表视图上方,或者将 VALUE 字符串添加为数组列表中的第一个条目,您可能会使用它来填充第二和第三个 activity 中的列表视图。
如果有人遇到这个问题,我无法将书籍和章节名称传递到标题或栏中,但我设法设置了一个标题。
在每个 activity 下的清单中,只需输入 android:label="title here"
至少得到一个你想要的标题。