【问题标题】:Trying to assign database information to ArrayList and then ListView尝试将数据库信息分配给 ArrayList 然后 ListView
【发布时间】:2014-01-30 13:59:06
【问题描述】:

所以请多多包涵……我正在尝试从我的数据库表中获取所有信息,并将该信息输出到一个不错的自定义 ListView(我已经构建)中。

MySQLiteHelper.java(我用来抓取信息的工具)

...
public List<String> getAllLogs() {
        List<String> List = new ArrayList<String>();
        String selectQuery = "SELECT * FROM " + TABLE_GASLOG;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()){
        do {
            List.add(cursor.getString(1));

        } while (cursor.moveToNext());
    }
    return List;
}
...

gasLog.java(我用来获取/设置我的所有信息)

...
public class gasLog {

    private int id;
    private double pricePerGallon;
    private double gallons;
    private double odometer;
    private String date;
    private String filledOrNot; //This will be a 0 or 1 value.
    private String comments;

    public gasLog(){}

    public gasLog(double pricePerGallon, double gallons, double odometer, String date, String filledOrNot, String comments){
        super();
        this.id = id;
        this.pricePerGallon = pricePerGallon;
        this.gallons = gallons;
        this.odometer = odometer;
        this.date = date;
        this.filledOrNot = filledOrNot;
        this.comments = comments;
    }

    //getters & setters


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getPricePerGallon() {
        return pricePerGallon;
    }

    public void setPricePerGallon(double pricePerGallon) {
        this.pricePerGallon = pricePerGallon;
    }

    public double getGallons(){
        return gallons;
    }

    public void setGallons(double gallons){
        this.gallons = gallons;
    }

    public double getOdometer(){
        return odometer;
    }

    public void setOdometer(double odometer){
        this.odometer = odometer;
    }

    public String getDate(){
        return date;
    }

    public void setDate(String date){
        this.date = date;
    }

    public String getFilledOrNot(){
        return filledOrNot;
    }

    public void setFilledOrNot(String filledOrNot){
        this.filledOrNot = filledOrNot;
    }

    public String getComments(){
        return comments;
    }

    public void setComments(String comments){
        this.comments = comments;
    }

    public String toString() {

        return "Date: " + date + ", Price: " + pricePerGallon + ", " + gallons + " Gallons, " + 
                ", Odometer Reading: " + odometer +
                ", Full fill: " + filledOrNot;
    }

}

history.java(我在其中扩充视图并将所有信息调用到)。

...
public class history extends ListActivity {

        // Log table name
        private static final String TABLE_GASLOG = "gasLog";


        // Log table columns names
        private static final String KEY_ID = "id";
        private static final String KEY_PRICE_PER_GALLON = "pricePerGallon";
        private static final String KEY_GALLONS = "gallons";
        private static final String KEY_ODOMETER = "odometer";
        private static final String KEY_DATE = "date";
        private static final String KEY_FILLED_OR_NOT = "filledOrNot";
        private static final String KEY_COMMENTS = "comments";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        Typeface tf = Typeface.createFromAsset(getAssets(),"Roboto-Light.ttf");

        ListView listContent = (ListView) findViewById(android.R.id.list);
        TextView history = (TextView) findViewById(R.id.history);
        history.setTypeface(tf);



        MySQLiteHelper db = new MySQLiteHelper(this);
        List<gasLog> list = new ArrayList<gasLog>();
        list = db.getAllLogs();
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        listContent.setAdapter(adapter);

    }

}

所以我知道我在 history.java 的底部需要更多类似于..的东西。

// THE DESIRED COLUMNS TO BE BOUND
            String[] columns = new String[] { People.NAME, People.NUMBER };
            // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
            int[] to = new int[] { R.id.name_entry, R.id.number_entry };

但我不太确定一切应该是什么。我曾尝试使用 getContentResolver,但我不确定如何设置 URI(或获取我的数据库的 URI),或者这是否是正确的方法。

我的 history.xml 包含一个列表视图,我有一个 bg.xml 文件,其中包含列表视图中每条记录的布局。现在我只能让它返回gasLog.java底部看起来凌乱的String toString()

任何帮助将不胜感激,如果有人能给我一些指导,也许为什么?期待在这方面学习一些我可以应用的东西。太感谢了!对不起,我是个新手!

编辑:

只是想确保我清楚这部分。 在 GasCursorAdapter.java 我将像这样设置 bindView:

public void bindView(View v, Context context, Cursor cursor){
        TextView cardDate = (TextView) v.findViewById(R.id.cardDate);
        int date = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_DATE);
        cardDate.setText(date);
    }

并且将为每个视图/数据库字段执行此操作(然后将值分配给视图)(我认为?!)

就这部分而言,我不太确定该放在哪里..

MySQLiteHelper db = new MySQLiteHelper(this);    
Cursor cursor = db.getAllLogs();
        GasCursorAdapter adapter = new GasCursorAdapter(content, cursor, 0);
        setAdapter(adapter);

现在我在 history.java 中有它,但我得到 内容无法解析为变量

除此之外,我认为感谢您,我已经弄清楚了!!!

再次感谢您的帮助!

【问题讨论】:

  • 您可能想使用CursorAdapter,但那是另一回事了。

标签: android sql sqlite android-listview cursor


【解决方案1】:

我建议您使用 CursorAdapter,它会跳过一些步骤并且运行得更顺畅。我正在做与您类似的事情,并且遇到了很多性能问题,但我切换到了 cursorAdapter,因此我的代码更容易理解和更快。有几个部分可以实现这一点,我将在下面向您展示。第一步是简单地返回游标,而不是尝试在数据库调用中处理它。

public Cursor getAllLogs() {
        List<String> List = new ArrayList<String>();
        String selectQuery = "SELECT * FROM " + TABLE_GASLOG;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    return cursor;
}

这实际上会以一种您可以更好地利用它的方式传递查询。我管理程序的方式是从这里做这样的事情。

  1. 定义数据库的一行输出应该是什么样子,并放在一个 XML 文件中。
  2. 设置类似于以下代码的 CursorAdapter。
  3. 创建查询,然后将其传递给新的 GasCursorAdapter。

这里是GasCursorAdapter

public class GasCursorAdapter extends CursorAdapter {

    private LayoutInflater mInflater=null;


    public GasCursorAdapter (Context context, Cursor c,int flags) {
        super(context, c, flags);
        mInflater=(LayoutInflater) context
               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return mInflater.inflate(R.layout.gasLayout,parent,false);
    }
    @Override
    public void bindView(View v,Context context, Cursor cursor) {
        //View is the view created by newView, take it and find your views and populate it, given the Cursor
    }

}

创建它看起来像:

Cursor cursor=getAllLogs();
GasCursorAdapter adapter=new GasCursorAdapter(context, cursor,0);
setAdapter(adapter);

【讨论】:

  • 谢谢皮尔森!这就是我正在寻找的东西,我现在将尝试实现它。请耐心等待,因为在此过程中我可能会遇到一些问题,并且会在我完成实施后立即将您的答案标记为已接受...我相信这是我再次需要的反馈,非常感谢!
  • 没问题。很高兴让您免于遭受我刚刚经历的痛苦。
  • Pearson,如果我能得到一点澄清,我发布了一个额外的问题。再次非常感谢您!我想我正在去那里的路上!
  • @Rob:有一个错字,我应该输入上下文。我不知道你从哪里得到它,但你必须将上下文传递给适配器创建。
  • 好的,谢谢你.. 我会做更多的挖掘来尝试并准​​确地了解需要在那里的东西!
猜你喜欢
  • 2015-11-10
  • 1970-01-01
  • 2016-07-01
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多