对此有困难。我想使用数据库将数据从添加提醒页面获取到包含列表视图的提醒页面。
这是数据库文件
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MOSDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
public static final String TABLE_NAME = "ReminderTable";
private static final String TABLE_CREATE = "create table " + TABLE_NAME + " (" +
"PTITLE" + " TEXT ," + //HEADING FOR MINUTE SELECTED IN TIMEPICKER
"PA" + " TEXT ," + //PAYMENT VALUE
"PDAY" + " TEXT ," + //DAY SELECTED IN DATEPICKER
"PMONTH" + " TEXT ," + //MONTH SELECTED IN DATEPICKER
"PYEAR" + " TEXT ," + //YEAR SELECTED IN DATEPICKER
"RDAY" + " TEXT ," + //REMINDER DAY
"RMONTH" + " TEXT ," + //REMINDER MONTH
"RYEAR" + " TEXT ," + //REMINDER YEAR
"RMIN" + " TEXT ," + //REMINDER MIN
"RHOUR" + " TEXT);"; //REMINDER HOUR
MOSDatabase(Context context){
super(context, "MOSDatabase", null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
this.onCreate(db);
}
}
这是我的提醒页面中使用的 CustomAdapter
import java.util.ArrayList;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<String> {
Cursor C;
SharedPreferences prefs;
String color;
String text_color;
String bgcolor;
public CustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
public ArrayList<String> paymentTitle;
public ArrayList<String> paymentDate;
public ArrayList<String> reminderTime;
public ArrayList<String> reminderDate;
public ArrayList<String> paymentAmount;
// Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
public CustomAdapter(Context context, int resource, ArrayList<String> pt, ArrayList<String> pd, ArrayList<String> rd, ArrayList<String> rt, ArrayList<String> pa) {
super(context, resource, pt);
this.paymentTitle = pt;
this.paymentDate = pd;
this.reminderDate = rd;
this.reminderTime = rt;
this.paymentAmount = pa;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.item_row, null);
}
//Get the corresponding string for the position of arraylist
String paymentTimeStr = paymentTitle.get(position).toString();
String paymentDateStr = paymentDate.get(position).toString();
String reminderDateStr = reminderDate.get(position).toString();
String reminderTimeStr = reminderTime.get(position).toString();
String paymentValueStr = paymentAmount.get(position).toString();
//FIND THE TEXTVIEWS FROM YOUR INDIVIDUAL LAYOUT
TextView payDateTV = (TextView) v.findViewById(R.id.txtPaymentDate);
TextView payTitleTV = (TextView) v.findViewById(R.id.txtTitle);
TextView reminderDateTV = (TextView) v.findViewById(R.id.txtReminderDate);
TextView reminderTimeTV = (TextView) v.findViewById(R.id.txtReminderTime);
TextView paymentValueTV = (TextView) v.findViewById(R.id.txtAmount);
if (payDateTV != null) {
payDateTV.setText(paymentDateStr);
}
if (payTitleTV != null) {
payTitleTV.setText(paymentTimeStr);
}
if (reminderDateTV != null) {
reminderDateTV.setText(reminderDateStr);
}
if (reminderTimeTV != null) {
reminderTimeTV.setText(reminderTimeStr);
}
if (paymentValueTV != null) {
paymentValueTV.setText(paymentValueStr);
}
return v;
}
}
这是提醒页面
public class Reminders extends Activity
{
TextView title;
Button add, edit, remove;
ListView mListView;
ArrayList<String> paymentTitle = new ArrayList<String>();
ArrayList<String> paymentDate = new ArrayList<String>();
ArrayList<String> reminderDate = new ArrayList<String>();
ArrayList<String> reminderTime = new ArrayList<String>();
ArrayList<String> paymentVal = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reminders);
initializeVariables();
MOSDatabase mDatabase = new MOSDatabase(this);
//DEFINE CUSTOM ADAPTER
CustomAdapter aa = new CustomAdapter(this, R.layout.item_row, paymentTitle, paymentDate, reminderDate, reminderTime, paymentVal);
//SET CUSTOM ADAPTER FOR LISTVIEW
mListView.setAdapter(aa);
// READING A DB
SQLiteDatabase readableDB = mDatabase.getReadableDatabase();
Cursor C = readableDB.query("ReminderTable", new String[] { "PTITLE", "PA", "PDAY", "PMONTH", "PYEAR", "RDAY", "RMONTH",
"RYEAR", "RMIN", "RHOUR" }, null, null, null, null, null);
if (C == null)
{
title.setText("No Upcoming Payments");
}
else{
C.moveToFirst();
do {
String reminderDateStr = "";
String reminderTimeStr = "";
String paymentDateStr = "";
String paymentTitleStr = "";
String paymentValue = "";
//Build strings of each record to then be set to the listview
reminderDateStr = C.getString(5) + "/" + C.getString(6)+ "/" +C.getString(7);
reminderTimeStr = C.getString(8) + ":" + C.getString(9);
paymentDateStr = C.getString(2) + "/" + C.getString(3) + "/" + C.getString(4);
paymentTitleStr = C.getString(0);
paymentValue = C.getString(1);
//Add the new strings to the corresponding array
paymentTitle.add(paymentTitleStr);
reminderTime.add(reminderTimeStr);
paymentDate.add(paymentDateStr);
reminderDate.add(reminderDateStr);
paymentVal.add(paymentValue);
} while (C.moveToNext());
}
//CLOSE CURSOR AND DATABASE TO AVOID MEMORY LEAKS
C.close();
readableDB.close();
//TELL ADAPTER THERE IS NEW DATA SO UPDATE THE LISTVIEW ACCORDINGLY
aa.notifyDataSetChanged();
}
private void initializeVariables()
{
title = (TextView) findViewById(R.id.txtTitle);
add = (Button) findViewById(R.id.bAdd);
edit = (Button) findViewById(R.id.bEdit);
remove = (Button) findViewById(R.id.bRemove);
mListView = (ListView) findViewById(R.id.LVReminder);
}
public void addClicked(View view)
{
Intent openNewPayment = new Intent("com.studentbudget.ADDREMINDER");
startActivity(openNewPayment);
finish();
}
}
当我运行我的应用程序并单击提醒链接时,它没有打开并崩溃。这是日志:
08-04 23:02:59.798 10548-10548/com.studentbudget E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.studentbudget/com.studentbudget.Reminders}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.studentbudget.Reminders.onCreate(Reminders.java:66)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
... 11 more
第 66 行是这样的:
reminderDateStr = C.getString(5) + "/" + C.getString(6)+ "/" +C.getString(7);
我试过改变它,所以它是remindDateStr = "Test"; 它起作用了,所以 C.getString(num) 方法有问题,我不知道它是什么。
感谢任何帮助我解决此问题的人。