1

我有一个问题,我现在几天都无法解决。我已经创建了向数据库写入和检索数据的方法,但是我找不到一个问题。这是代码:

    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)

不知道问题出在哪里。请帮忙。

4

4 回答 4

0

您应该检查是否cursor.moveToFirst()返回 true。

如果您正在执行的查询没有任何结果,您将获得该CursorIndexOutOfBoundsException异常...

此外,正如 Aprian 指出的那样。cursor.getString将列索引作为参数,而不是光标的位置。

替换result.add(cursor.getString(cursor.getPosition()));result.add(cursor.getString(Database.KEY_STATS));

于 2012-09-27T08:05:26.093 回答
0

cursor.getString(cursor.getPosition())不会导致您发布的错误,但稍后会导致其他错误。如果您阅读文档,预期的参数是列索引,而不是行索引。

您观察到的错误可能是由于 moveToFirst 返回 False 但您没有对其进行测试。你的数据库是空的。

编辑

再读一遍后,你的 for 循环(虽然看起来很漂亮)完全关闭了。您将 isAfterLast 测试为循环条件,而充其量它应该是退出条件。您不检查移动到第一个的结果。您不检查 moveToNext 的结果。

将您的 for 循环替换为

while(cursor.moveToNext()) {
    // Do stuff
}
于 2012-09-27T08:09:10.587 回答
0

问题似乎也在这里:

result.add(cursor.getString(cursor.getPosition()));

因为如果获取的记录数多于查询中请求的列数,就会抛出错误。这不是您应该从游标中读取值的方式,实际上您应该确定它应该读取哪些列值。例如:

result.add(cursor.getString(cursor.getColumnIndex(Database.KEY_NAME)));

或者可能具有固定的索引值,例如:

result.add(cursor.getString(0));
于 2012-09-27T08:09:30.630 回答
0

编辑:似乎您想要的是从数据库中检索单行。

您应该使用MyObject而不是List<String>来实现您想要的。

这是一个例子MyObject,见POJO

public class MyObject {
    String name = "", stats = "";
    Long period = 0, frequency = 0, receivingtime = 0;

    // empty constructor
    public MyObject() {
    }

    // simple getter method
    public String getName() {
        return name;
    }

    // simple setter method
    public void setName(String name) {
        this.name = name;
    }

    ...
    // You may want to override toString() method when implementing searching or using ArrayAdapter
    @Override
    public String toString() {
        return name + " " + stats;
    }
}

更改getReportInfo为:

public MyObject getReportInfo(String rowId){
    MyObject result = new MyObject();
    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);
    // this will prevent CursorIndexOutofBoundsException
    if (cursor.moveToFirst()) {
        result.setName(cursor.getString(0));
        result.setPeriod(cursor.getLong(1));
        result.setFrequency(cursor.getLong(2));
        result.setReceivingTime(cursor.getLong(3));
        result.setStats(cursor.getString(4));
    }
    cursor.close(); 
    database.close();   
    return result;
}
于 2012-09-27T08:32:59.930 回答