android的sqlite的读取所有的行一次

android的sqlite的读取所有的行一次

本文介绍了android的sqlite的读取所有的行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法读取一个SQLite表中的所有行和一个TextView一次显示他们呢?
这是我阅读并逐行地读取....

  // ---检索所有的标题---
    公共光标getAllTitles()
    {
        返回db.query(DATABASE_TABLE,新的String [] {
                KEY_ROWID,
                KEY_ISBN,
                KEY_TITLE,
                KEY_PUBLISHER},
                空值,
                空值,
                空值,
                空值,
                空值);进口android.app.Activity;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.widget.Toast;公共类DatabaseActivity延伸活动{
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        DBAdapter DB =新DBAdapter(本);        // ---获得所有冠军---
        db.open();
        光标C = db.getAllTitles();
        如果(c.moveToFirst())
        {
            做{
                DISPLAYTITLE(C);
            }而(c.moveToNext());
        }
        db.close();
    }
} 公共无效DISPLAYTITLE(光标C)
    {
        Toast.makeText(这一点,
                ID:+ c.getString(0)+\\ N+
                ISBN:+ c.getString(1)+\\ n+
                标题:+ c.getString(2)+\\ n+
                PUBLISHER:+ c.getString(3)
                Toast.LENGTH_LONG).show();
    }


解决方案

首先,你可能想寻找到列表视图,可以轻松地显示数据的列表是这样的。

如果你的目标真的是一个TextView中显示所有信息(或作为敬酒你现在做),你可以尝试做一个大字符串,与您共创敬酒:

  // ---获得所有冠军---
    db.open();
    光标C = db.getAllTitles();
    字符串文本=;
    如果(c.moveToFirst())
    {
        做{
            DISPLAYTITLE(C,文本);
        }而(c.moveToNext());
    }
    db.close();
    Toast.makeText(这一点,文本,Toast.LENGTH_LONG).show();
}公共无效DISPLAYTITLE(光标C,字符串文本)
{
    文字+ =
            ID:+ c.getString(0)+\\ N+
            ISBN:+ c.getString(1)+\\ n+
            标题:+ c.getString(2)+\\ n+
            PUBLISHER:+ c.getString(3);
}

Is there a way to read all the rows in an sqlite table and display them at once in a textview?This is how I read them and it reads line by line ....

//---retrieves all the titles---
    public Cursor getAllTitles()
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID,
                KEY_ISBN,
                KEY_TITLE,
                KEY_PUBLISHER},
                null,
                null,
                null,
                null,
                null);

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;

public class DatabaseActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DBAdapter db = new DBAdapter(this);

        //---get all titles---
        db.open();
        Cursor c = db.getAllTitles();
        if (c.moveToFirst())
        {
            do {
                DisplayTitle(c);
            } while (c.moveToNext());
        }
        db.close();
    }
}

 public void DisplayTitle(Cursor c)
    {
        Toast.makeText(this,
                "id: " + c.getString(0) + "\n" +
                "ISBN: " + c.getString(1) + "\n" +
                "TITLE: " + c.getString(2) + "\n" +
                "PUBLISHER:  " + c.getString(3),
                Toast.LENGTH_LONG).show();
    }
解决方案

First of all, you might want to look into listviews to easily display a list of data like this.

If your goal really is to display all information in one textview (or toast as you're making now), you could try making one large string, with which you create the toast:

    //---get all titles---
    db.open();
    Cursor c = db.getAllTitles();
    String text = "";
    if (c.moveToFirst())
    {
        do {
            DisplayTitle(c, text);
        } while (c.moveToNext());
    }
    db.close();
    Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}

public void DisplayTitle(Cursor c, String text)
{
    text +=
            "id: " + c.getString(0) + "\n" +
            "ISBN: " + c.getString(1) + "\n" +
            "TITLE: " + c.getString(2) + "\n" +
            "PUBLISHER:  " + c.getString(3);
}

这篇关于android的sqlite的读取所有的行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:44