我有一个问题,我现在几天都无法解决。我已经创建了向数据库写入和检索数据的方法,但是我找不到一个问题。这是代码:
public void writeEntry(String name, long period, long frequency, long recievingTime, String stats){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_PERIOD, period);
cv.put(KEY_FREQUENCY, frequency);
cv.put(KEY_RECIEVINGTIME, recievingTime);
cv.put(KEY_STATS, stats);
myDatabase.insert(DATABASE_TABLE, null, cv);
}
public Cursor query(String[] columns, String selection, String[] selectionArgs, String groupBy) {
// TODO Auto-generated method stub
Cursor c = myDatabase.query(DATABASE_TABLE, columns, selection, selectionArgs, groupBy, null, null);
return c;
}
这是将数据写入数据库的方法:
public void save(){
sharedPrefs = context.getSharedPreferences(SPNAME, 0);
Log.v(" info", sharedPrefs.getAll().toString());
Database database = new Database(context);
database.open();
database.writeEntry(
sharedPrefs.getString(NAME, "No data was found"),
sharedPrefs.getLong(PERIOD, 0),
sharedPrefs.getLong(FREQUENCY, 0),
sharedPrefs.getLong(RECIEVINGTIME, 0),
sharedPrefs.getString(STATS, "No data was found")
);
database.close();
}
这是我检索它的方法:
public List<String> getReportInfo(String rowId){
List<String> result = new ArrayList<String>();
database = new Database(context);
database.open();
cursor = database.query(new String[]{
Database.KEY_NAME,
Database.KEY_PERIOD,
Database.KEY_FREQUENCY,
Database.KEY_RECIEVINGTIME,
Database.KEY_STATS},
Database.KEY_ROWID +" = "+rowId, null, null);
for(cursor.moveToFirst(); cursor.isAfterLast(); cursor.moveToNext()){
result.add(cursor.getString(cursor.getPosition()));
}
cursor.close();
database.close();
return result;
}
稍后,我在 getView 方法中使用 BaseAdapter 中的 db 数据来填充 ListView,如下所示:
public class MyArrayAdapter extends BaseAdapter{
private Activity activity;
private LayoutInflater inflater;
TextView tvMyReportsPeriod, tvMyReportsFrequency, tvMyReportsReceivingTime, tvMyReportTitle;
LinearLayout llMyReportsStats;
Database database;
Report report;
Cursor cursor;
public MyArrayAdapter(Activity a) {
super();
// TODO Auto-generated constructor stub
activity = a;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
report = new Report(activity);
}
public int getCount() {
// TODO Auto-generated method stub
int count = 2;
try{
cursor = database.query(new String[]{Database.KEY_NAME}, null, null, null);
count= cursor.getCount();
cursor.close();
}catch(Exception e){}
return count;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
List<String> list = report.getReportInfo(String.valueOf(position));
return list;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
convertView = inflater.inflate(R.layout.my_reports_view_holder, null, false);
tvMyReportsPeriod = (TextView)convertView.findViewById(R.id.tvMyReportsPeriod);
tvMyReportsFrequency = (TextView)convertView.findViewById(R.id.tvMyReportsFrequency);
tvMyReportsReceivingTime = (TextView)convertView.findViewById(R.id.tvMyReportsReceivingTime);
tvMyReportTitle = (TextView)convertView.findViewById(R.id.tvMyReportTitle);
llMyReportsStats = (LinearLayout)convertView.findViewById(R.id.llMyReportsStats);
List<String> data = report.getReportInfo(String.valueOf(position));
if(data!=null){
tvMyReportTitle.setText(data.get(0));
tvMyReportsPeriod.setText(report.convertMillis(Long.parseLong(data.get(1))));
tvMyReportsFrequency.setText(report.convertMillis(Long.parseLong(data.get(2))));
tvMyReportsReceivingTime.setText(report.convertMillis(Long.parseLong(data.get(3))));
String[] stats = data.get(4).toString().split(" ");
for(int x=0; x<stats.length; x++){
TextView textView = new TextView(activity);
for(Constants dc: Constants.values()){
if(dc.getId()==Integer.parseInt(stats[x])){
textView.setText(dc.getText());
}
}
llMyReportsStats.addView(textView);
}
}else{System.out.println("ERROR: data=null");}
}
return convertView;
}
我希望为每个 ListView 项目一次从 db 中获取一行,但是我得到了这个 logcat:
09-27 11:03:03.173: E/AndroidRuntime(25768): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
09-27 11:03:03.173: E/AndroidRuntime(25768): at com.dailyreports.ainius.Report.getReportInfo(Report.java:107)
09-27 11:03:03.173: E/AndroidRuntime(25768): at com.dailyreports.ainius.MyReports$MyArrayAdapter.getView(MyReports.java:88)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.ListView.onMeasure(ListView.java:1127)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.ViewRoot.performTraversals(ViewRoot.java:841)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.view.ViewRoot.handleMessage(ViewRoot.java:1861)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.os.Looper.loop(Looper.java:123)
09-27 11:03:03.173: E/AndroidRuntime(25768): at android.app.ActivityThread.main(ActivityThread.java:3729)
09-27 11:03:03.173: E/AndroidRuntime(25768): at java.lang.reflect.Method.invokeNative(Native Method)
09-27 11:03:03.173: E/AndroidRuntime(25768): at java.lang.reflect.Method.invoke(Method.java:507)
09-27 11:03:03.173: E/AndroidRuntime(25768): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
09-27 11:03:03.173: E/AndroidRuntime(25768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
09-27 11:03:03.173: E/AndroidRuntime(25768): at dalvik.system.NativeStart.main(Native Method)
不知道问题出在哪里。请帮忙。