gpt4 book ai didi

android - 尝试将数据库信息分配给 ArrayList,然后分配给 ListView

转载 作者:行者123 更新时间:2023-11-29 21:15:56 25 4
gpt4 key购买 nike

所以请耐心等待我...我正在尝试从我的数据库表中获取所有信息并将该信息输出到一个漂亮的自定义 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(我在这里膨胀 View 并将所有信息调用到)。

...
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 包含一个 ListView ,我有一个 bg.xml 文件,其中包含 ListView 中每条记录的布局。现在我只能让它返回 gasLog.java 底部看起来凌乱的字符串 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);
}

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

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

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

现在我在 history.java 中有它,但我得到 content cannot be resolved to a variable

除此之外,我认为我已经想通了,谢谢你!!!

再次感谢您的帮助!

最佳答案

我建议您使用 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);

关于android - 尝试将数据库信息分配给 ArrayList,然后分配给 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21458506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com