游标变量无法识别

时间:2014-06-01 04:15:11

标签: android variables android-sqlite

我在sqlite中有一个int数据我试图访问以显示活动中的状态。以下是数据库帮助程序活动中带有getCA()游标的活动页面。

我在尝试获取数据方面遇到了麻烦,特别是“第一个”数字,它是一个int作为在同一活动中在apple类之外共享的变量。正如你在下面看到的那样,我试图通过首先将int变量df(设置为0,因此它对第一个没有影响)添加到int变量来“欺骗”,所以我基本上可以得到第一个数字。

但是这个新的dfe变量在apple类的内部或外部都无法识别?我试过把它变成'public int',但它说公共在这种情况下无效。

      protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    myDb= new DbA(this);
    myDb.open();                
    setContentView(R.layout.mainpage);
            getdata();         
              }

    private void getdata() {
        Cursor cursor = myDb.getCA();
        apple(cursor);              
    }


    private void apple(Cursor cursor) {
        int df = 0;
        if (ur.moveToFirst()) {
            do {
                int first = cursor.getInt(0);
                int coma = cursor.getInt(1);
                String desc = cursor.getString(2);

                // Append data to the message:
                int dfe = df + first;                      

            } while(cursor.moveToNext());

        }

        cursor.close();

        ///this last line below, eclipse cannot recognize 'dfe' as a variable?
        int cot = dfe;
        }

请帮忙。你可能会说,我对android很新。

我想基本上问题是如何与同一活动页面中的其他类共享从apple类中的数据库派生的变量?提前谢谢。

2 个答案:

答案 0 :(得分:0)

int dfe是在循环中定义的,因此变量只能在循环中访问,将其更改为方法变量

// Append data to the message:
   int dfe = df + first;  

修正:使用此方法

private void apple(Cursor cursor) {
        int df = 0;
        int dfe;
        if (cursor.moveToFirst()) {
            do {
                int first = cursor.getInt(0);
                int coma = cursor.getInt(1);
                String desc = cursor.getString(2);

                // Append data to the message:
                    dfe = df + first;                      

            } while(cursor.moveToNext());

        }

        cursor.close();

        int cot = dfe;
    }

答案 1 :(得分:0)

看起来你也是Java新手。 ;)

在您的代码中,apple不是一个类,它是一种方法 你用class Name定义类,它应该在一个自己的文件中(除非你想要一个内部类,但我认为你不要)。

您收到该错误的原因是因为方法不在其字段上设置可见性修饰符,它们对方法是私有的(或者在声明它们的scope内。如果您想要访问在方法(或循环)内外的变量,你应该在包含的类或方法中声明它 您的dfe变量也是如此,它在do...while循环内声明,无法在其外部显示。

您应该阅读variable scope来解决您的问题。


我不确定你是否混淆了一个叫apple类的术语,或者你是否真的希望它作为一个类。如果你确实想要它作为一个类,你可以做到这一点:

public Apple {
    // declare the fields you want to store for Apple
    private int dfe;
    private String description;

    public Apple(int first, int coma, String description)
        // use the arguments to set your variables
        this.dfe = //some calculation
        this.description = description;
    }

    // define methods for accessing the fields
    public int getDfe() {
        return this.dfe;
    }
    public String getDescription() {
        return this.description;
    }
}

然后在读取光标的方法中:
(我假设它包含多个结果)

public List<Apple> getDemApples(Cursor cursor) {
    List<Apple> apples = new ArrayList<Apple>();
    if (cursor != null) {
        int tempFirst;
        int tempComa;
        String tempDescription;

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            tempFirst = cursor.getInt(0);
            tempComa = cursor.getInt(1);
            tempDescription = cursor.getString(2);
            apples.add(new Apple(tempFirst, tempComa, tempDescription));
            cursor.moveToNext();
        }
    }
    return apples;
}

或者那些东西。同样,我可能误解了你想要做的事情。

相关问题