没有返回正确的ID

时间:2014-07-07 08:37:15

标签: android database sqlite

所以我试图通过他们在课堂上收集“积分”来修改学生的参与度。这在考勤表中完成,每个学生都有一个出勤ID作为PK。

这是我的问题:光标c,它应该从出勤表中返回ID,而是返回“1”,“2”和“3”之类的数字,这些数字似乎特定于(可能)运行的应用程序,而不是查询数据库并在那里返回ID。我知道这是因为我已经获取了数据库文件,并使用数据库浏览器进行了检查。数据库中的ID是正确的。光标未检索到正确的ID。

TADADataDbHelper dbreadhelper = new TADADataDbHelper(this);
    SQLiteDatabase dbread = dbreadhelper.getWritableDatabase();

    Bundle bundle = getIntent().getExtras();
    final long meeting = bundle.getLong("meeting");

    String sql = "select * " +
                " from "  + Meetings.TABLE_NAME +
                " , " + Attendance.TABLE_NAME +
                " , " + Student.TABLE_NAME +
                " where " + Meetings.TABLE_NAME+"."+Meetings._ID + "= '" + meeting + "'" +
                " and " + Student.COLUMN_NAME_STUDENTID + "=" + Attendance.COLUMN_NAME_STUDENT +
                " and " + Attendance.COLUMN_NAME_PRESENT + "=1" +
                " and " + Attendance.COLUMN_NAME_MEETING + "=" + Meetings.TABLE_NAME+"."+Meetings._ID;

    Cursor c = dbread.rawQuery(sql, null);

    String[] fromfields = {Student.COLUMN_NAME_STUDENTNAME,Student.COLUMN_NAME_STUDENTNAME,Attendance.COLUMN_NAME_PARTICIPATION,Student.COLUMN_NAME_STUDENTNAME};
    int[] tofields = {R.id.addpartbutton,R.id.studentnametext,R.id.studentpointstext,R.id.subpartbutton};

    try {
        SimpleCursorAdapter adapt2 = new SimpleCursorAdapter(this, R.layout.participationlistitem, c, fromfields, tofields);


        adapt2.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                // TODO Auto-generated method stub
                Log.i("Meedz","Entered");
                if(!(view instanceof Button))
                    return false;

                Log.i("Meedz","Went on");

                final Button controller = (Button) view;

                final Cursor c = cursor;

                if (controller.getId()==R.id.addpartbutton)
                    controller.setText("+");
                else
                    controller.setText("-");

                final long attid = c.getLong(c.getColumnIndex("attendance._id"));
                final String name = c.getString(c.getColumnIndex("student.studentname"));

                controller.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        TADADataDbHelper dbreadhelper = new TADADataDbHelper(getApplicationContext());
                        SQLiteDatabase dbread = dbreadhelper.getWritableDatabase();

                        if (controller.getId()==R.id.addpartbutton)
                        {
                            Log.i("Meedz","Adding "+attid+name);

这最后一行是显示出勤ID的内容。它显示简单的数字,而实际数据库则不是这样。

                            }
                            else
                            {

                            Log.i("Meedz","Substracting "+attid);
                        }


                    }
                });


                return true;
            }
        });


        ListView mylist = (ListView)findViewById(R.id.participationlist);
        mylist.setAdapter(adapt2);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this, "Error3", Toast.LENGTH_LONG).show();

    }
}

谢谢

2 个答案:

答案 0 :(得分:0)

在你的代码中,我认为'attid'只是结果中的索引,因为你调用了getColumnIndex。因此,您需要调用实际的值检索函数,如cursor.getLong(...)。

答案 1 :(得分:0)

这是我发现的,为了别人的利益......

如果您有_ID,光标将尝试使用它进行索引。那么,你应该怎么做?在我的情况下,它很容易,因为我有多个_ID(因为我正在进行连接),我只是将其中一个作为_ID,并给出了其余的名称。然后,我稍后提到了该名称,它正在从数据库中检索正确的ID。这很难搞清楚;花了几天时间。我希望这能帮助那些阅读它的人。

我的选择语句更改如下:

String sql = "select " +
                            Student.COLUMN_NAME_STUDENTID + COMMA + 
                            Student.COLUMN_NAME_STUDENTNAME + COMMA +
                            Student.TABLE_NAME + "." + Student._ID + " " + COMMA +
                            Attendance.COLUMN_NAME_MEETING + COMMA +
                            Attendance.COLUMN_NAME_PARTICIPATION + COMMA +
                            Attendance.COLUMN_NAME_PRESENT + COMMA +
                            Attendance.COLUMN_NAME_STUDENT + COMMA +
                            Attendance.TABLE_NAME + "." + Attendance._ID + "newattendanceid" + COMMA +
                            Meetings.COLUMN_NAME_CODE + COMMA +
                            Meetings.COLUMN_NAME_DATE + COMMA +
                            Meetings.COLUMN_NAME_MAJOR + COMMA +
                            Meetings.COLUMN_NAME_SECTION + COMMA +
                            Meetings.TABLE_NAME + "." + Meetings._ID + " newmeetingid" +
                " from "  + Meetings.TABLE_NAME +
                " , " + Attendance.TABLE_NAME +
                " , " + Student.TABLE_NAME +
                " where newmeetingid = '" + meeting + "'" +
                " and " + Student.COLUMN_NAME_STUDENTID + "=" + Attendance.COLUMN_NAME_STUDENT +
                " and " + Attendance.COLUMN_NAME_PRESENT + "=1" +
                " and " + Attendance.COLUMN_NAME_MEETING + "= newmeetingid";