我正在尝试将我的数据库的内容显示在ListView
中。有一些错误(很明显),它在数据库中多次返回#1行。
例如,如果有2行,则返回#1行2次..如果我添加另一条记录,则返回#1行3次。
这是我的 history.java
...
MySQLiteHelper db = new MySQLiteHelper(this);
Cursor cursor = db.getAllLogs();
Log.d("history.java", "finished Cursor cursor = db.getAllLogs();");
GasCursorAdapter adapter = new GasCursorAdapter(this, cursor, 0);
Log.d("history.java", "GasCursorAdapter adapter = new GasCursorAdapter(this, cursor, 0);");
listContent.setAdapter(adapter);
Log.d("history.java", "setAdapter(adapter);");
这是我的 MySQLiteHelper.java
...
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;
}
我猜这是getAllLogs()
中我需要做的事情......但是我无法确定它。我曾尝试使用cursor.moveToFirst()
和moveToNext
,但这似乎会产生相同的结果......似乎知道有多条记录,但只是多次返回第一条记录!
非常感谢任何帮助!
编辑(附加信息) 的 newRecord.java
public class newRecord extends Activity {
String fill;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_record);
Typeface tf = Typeface.createFromAsset(getAssets(),"Roboto-Light.ttf");
final MySQLiteHelper db = new MySQLiteHelper(this);
final EditText pricePerGallon = (EditText) findViewById(R.id.pricePerGallon);
final EditText gallons = (EditText) findViewById(R.id.gallons);
final EditText odometer = (EditText) findViewById(R.id.odometer);
final EditText today = (EditText) findViewById(R.id.date);
final EditText comments = (EditText) findViewById(R.id.comments);
final CheckBox notCompleteFill = (CheckBox) findViewById(R.id.completeFill);
TextView save = (TextView) findViewById(R.id.save);
TextView reset = (TextView) findViewById(R.id.reset);
pricePerGallon.setTypeface(tf);
gallons.setTypeface(tf);
odometer.setTypeface(tf);
today.setTypeface(tf);
comments.setTypeface(tf);
notCompleteFill.setTypeface(tf);
save.setTypeface(tf);
reset.setTypeface(tf);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
today.setText( sdf.format(new Date()));
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
String pricePerGallonString = pricePerGallon.getText().toString();
final double pricePerGallonInt = Double.parseDouble(pricePerGallonString);
String gallonsString = gallons.getText().toString();
final double gallonsInt = Double.parseDouble(gallonsString);
String odometerString = odometer.getText().toString();
final double odometerInt = Double.parseDouble(odometerString);
String todayString = today.getText().toString();
String notCompleteFillString = notCompleteFill.getText().toString();
String commentsString = comments.getText().toString();
db.addGasLog(new gasLog(pricePerGallonInt, gallonsInt, odometerInt, todayString, fill, commentsString));
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_LONG).show();
Intent i = new Intent(newRecord.this, history.class);
startActivity(i);
}
});
MySQLiteHelper.java
public void addGasLog(gasLog gasLog){
// for logging
Log.d("addGasLog", gasLog.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_PRICE_PER_GALLON, gasLog.getPricePerGallon()); // get price
values.put(KEY_GALLONS, gasLog.getGallons()); // get gallons
values.put(KEY_ODOMETER, gasLog.getOdometer()); // get odometer
values.put(KEY_DATE, gasLog.getDate()); // get date
values.put(KEY_FILLED_OR_NOT, gasLog.getFilledOrNot()); // get filledOrNot
values.put(KEY_COMMENTS, gasLog.getComments()); // get comments
// 3. insert
db.insert(TABLE_GASLOG, //table
null, //nullColumnHack
values); // key/value -> keys = column names/ values =
// 4. Close
db.close();
}
EDIT2 的 GasCursorAdapter.java
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
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);
Log.d("inGasCursorAdapter", "GasCursorAdapter");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.card_background, parent, false);
Log.d("inGasCursorAdapter", "newView");
return retView;
//return mInflater.inflate(R.layout.card_background, parent, false);
}
@Override
public void bindView(View v, Context context, Cursor cursor){
/*
* cardDate
* cardGallons
* cardPrice
* cardMPG
*/
TextView cardDate = (TextView) v.findViewById(R.id.cardDate);
int date = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_DATE);
cardDate.setText(Integer.toString(date));
TextView cardGallons = (TextView) v.findViewById(R.id.cardGallons);
int gallons = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_GALLONS);
cardGallons.setText(Integer.toString(gallons) + "gal");
TextView cardPrice = (TextView) v.findViewById(R.id.cardPrice);
int price = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_PRICE_PER_GALLON);
cardPrice.setText("$" + Integer.toString(price));
TextView cardMPG = (TextView) v.findViewById(R.id.cardMPG);
int MPG = cursor.getColumnIndexOrThrow(MySQLiteHelper.KEY_ODOMETER);
cardMPG.setText(Integer.toString(MPG));
TextView cardDelete = (TextView) v.findViewById(R.id.cardDelete);
cardDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
}
});
}
}
答案 0 :(得分:0)
你的表有一个名为_id的列吗?如果不是,您可能需要选择单个列(无论如何应该选择而不是选择*),并将您的密钥主键别名为_id。