SQLite-在ListView上方的标题(源代码与ListView源相同)

时间:2016-09-15 03:59:46

标签: android sqlite listview android-sqlite

我忙于圣经应用程序,我使用资产文件夹中的SQLite数据库来检索数据,我使用3个列表视图,每个都在自己的活动中,它是这样的:

数据库列名称:图书名称,图书ID,章节编号,章节ID,经文,诗句

第一项活动,用户选择一本书,Genesis,Exodus,Leviticus等......第二项活动开始

第二个活动,db被过滤,因此用户可以选择该书下的章节......并开始第三个活动

第3次活动,db被过滤并显示该章下的所有经文......

我想要的是将文本视图放在第二个列表视图的顶部,然后在用户从第一个列表视图中选择并且第二个活动开始显示所有章节之后,它应该显示从中选择的书名第一项活动。在第3个列表视图中,它应显示从前两个活动中选择的书名和章节号,我尝试过使用打算,但是我收到了错误。

适配器:

position

应显示的活动:

    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;
        }
        }

DBHandler:

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);
                }
            });
        }
    }

显示它的XML:

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;
}
}

2 个答案:

答案 0 :(得分:0)

您可以使用

通过意图发送字符串
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); 

并使用

获取第二个活动中的字符串
Intent intent = getIntent();
if(intent!=null)
    String VALUE = intent.getExtras().getString(KEY);

现在在textview中设置VALUE字符串。此外,您需要将chapterName textview放在listview上方,或者将VALUE字符串添加为arraylist中的第一个条目,您可能在第二个和第三个活动中填充listview。

答案 1 :(得分:0)

如果有人可能会遇到这个问题,我无法将书籍和章节名称传递给标题或栏目,但我能够想出设置标题。

在每个活动的清单中,只需输入android:label="title here" 至少得到你想要的标题或标题。