How to set each item retrieved from SQLite database into a Textview of it's own

前端 未结 1 1603
忘掉有多难
忘掉有多难 2021-01-29 08:38

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 w

相关标签:
1条回答
  • 2021-01-29 09:10

    try this

    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;
        }
    
    0 讨论(0)
提交回复
热议问题