如何将从SQLite数据库检索到的每个项目设置为它自己的Textview [英] How to set each item retrieved from SQLite database into a Textview of it's own

查看:109
本文介绍了如何将从SQLite数据库检索到的每个项目设置为它自己的Textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有一个数据库,该数据库的单个表具有TEXT类型的单列.我希望能够从该表中检索数据,对于该表中的每个字符串,我都希望将其插入到它自己的TextView中.我希望TextViews以垂直LinearLayout的方式进行排列.我知道检索到的数据是在使用ListView的活动中呈现的,但是我想为每个文本提供单独的TextView,因为以后我想将每个TextView设置为可点击并为每个文本提供一些功能.请帮助我提供代码,并告诉我是否需要在问题中包含任何项目文件.

For now, I have a database with a single table having a single column of type TEXT. I want to be able to retrieve the data from that table and for each string from the table I want to insert it into it's own TextView. I want the TextViews to be arranged in vertical LinearLayout fashion. I know retrieved data is presented in an activity using ListView but I want separate TextView for each because I later want to set each TextView clickable and give each one some function. Please help me with the code and tell me if any project files need to be included in the question.

推荐答案

尝试一下

Cursor 中的 Database 中获取数据,并将光标移到下面的方法中,它将返回一个 view 您可以传入 setContentView(view)

Fetch data from Database in Cursor and pass this cursor in below method it will return a view which you can pass in setContentView(view)

 public LinearLayoutCompat defaultPage(Cursor mCursor) {
        LinearLayoutCompat layout = new LinearLayoutCompat(this);

        layout.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT));
        layout.setGravity(Gravity.CENTER);
        layout.setOrientation(LinearLayoutCompat.VERTICAL); 
        for(int i=0;i<mCursor.getCount();i++) {
            mCursor.moveToPosition(i);
            AppCompatTextView defaultText = new AppCompatTextView(this);
            defaultText.setText(mCursor.getString(mCursor.getColumnIndex("TOUR_COLUMN_NMAE")));
            defaultText.setTextSize(20);
            defaultText.setTypeface(null, Typeface.BOLD);
            defaultText.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // Toast.makeText(mContext, "Work in progress", Toast.LENGTH_SHORT).show();
                }
            });
            layout.addView(defaultText);
        }    
        return layout;
    }

这篇关于如何将从SQLite数据库检索到的每个项目设置为它自己的Textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆