Java Android Studio存在静态方法声明问题
我目前正在尝试创建一个项目,该项目将创建一个包含3个元素的数据库:ID、名称和状态。然后我需要将所有这些值存储到3个列表视图中,这些视图都位于同一个活动中。以下是我的sqlite代码:Java Android Studio存在静态方法声明问题,java,static,Java,Static,我目前正在尝试创建一个项目,该项目将创建一个包含3个元素的数据库:ID、名称和状态。然后我需要将所有这些值存储到3个列表视图中,这些视图都位于同一个活动中。以下是我的sqlite代码: package com.example.mikediloreto.sqliteproject; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; impor
package com.example.mikediloreto.sqliteproject;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
public class myDB extends SQLiteOpenHelper{
private static final int DB_VERSION = 1;
private static final String TB_Student = "student";
public static final String ID = "id";
public static final String NAME = "name";
public static final String STATUS = "status";
public myDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create Table " + TB_Student + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + NAME + " TEXT NOT NULL, " + STATUS + " TEXT" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public Cursor getStudents() {
String[] cols = new String[] { ID, NAME, STATUS};
SQLiteDatabase db = getReadableDatabase();
return db.query(TB_Student, cols, null, null, null, null, NAME);
}
public Cursor getStudent(int studentId) {
String[] cols = new String[] { ID, NAME, STATUS};
String sel = ID + "=?";
String[] selArgs = new String[] { String.valueOf(studentId)};
SQLiteDatabase db = getReadableDatabase();
return db.query(TB_Student, cols, sel, selArgs, null, null, null);
}
public long numStudents() {
SQLiteDatabase db = getReadableDatabase();
SQLiteStatement st = db.compileStatement("SELECT COUNT(1) " + "FROM "+ TB_Student + ";");
return st.simpleQueryForLong();
}
public long addStudent(String name, String status) {
ContentValues cv = new ContentValues();
cv.put(NAME, name);
cv.put(STATUS, status);
SQLiteDatabase db = getWritableDatabase();
return db.insert(TB_Student, null, cv);
}
public boolean delStudent(int studentId) {
String sel = ID + "=?";
String[] selArgs = new String[] { String.valueOf(studentId) };
SQLiteDatabase db = getReadableDatabase();
return (db.delete(TB_Student, sel, selArgs) > 0);
}
}
以下是我的主要活动:
package com.example.mikediloreto.sqliteproject;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.*;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv1=(ListView)this.findViewById(R.id.leftListView);
ListView lv2=(ListView)this.findViewById(R.id.centerListView);
ListView lv3=(ListView)this.findViewById(R.id.rightListView);
setListAdapter(new SimpleCursorAdapter(this, R.layout.*, myDB.getStudents(), new String [] { myDB.ID, myDB.NAME, myDB.STATUS }, new int[] { android.R.id.text1 }, 0));
}
}
我遇到的问题是,当我调用myDB.getStudents()时,它告诉我需要将方法声明设置为静态,但如果将其设置为静态,则会出现一个错误,告诉我getReadableDatabase()不能是静态的。不知道从这里走到哪里,如果还有其他明显的错误,请告诉我
另外,控制台告诉我在使用R.layout.*时出现错误,修复方法是将其放在getStudents()调用之后
编辑:创建实例变量有效。谢谢你的帮助
您的问题是不能使用类名访问类中的非静态
公共
方法
- 因此,您可以使用
并使用myDB mMyDB
访问Class对象
方法getStudents
- 您可以使您的方法
静态
myDB
中,您可以更改为此
public myDB(Context contextr) {
super(context, NAME , null , DB_VERSION , mull);
}
在您的main活动中
public class MainActivity extends ListActivity {
myDB mMyDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv1=(ListView)this.findViewById(R.id.leftListView);
ListView lv2=(ListView)this.findViewById(R.id.centerListView);
ListView lv3=(ListView)this.findViewById(R.id.rightListView);
mMyDB = new myDB(this);
// edited here , change the layout
setListAdapter(new SimpleCursorAdapter(this, R.layout.your_layout, mMyDB.getStudents(), new String [] { myDB.ID, myDB.NAME, myDB.STATUS }, new int[] { android.R.id.text1 }, 0));
}
}
编辑
您应该将R.layout。您的_布局
更改为您现有的布局
注意
然后修改你的类名,这种书写不是很规范
类似于MyDB的样本
第一个字母的类名应该是大写。尝试过,但没有任何区别。仍然有同样的问题。这不是一个坏答案,但是如果你能向OP解释实例化一个对象意味着什么,可能会有所帮助。谢谢,这就解决了它。我仍然收到错误,但这是由R.layout.*引起的,它产生了两个错误,预期的和非法的表达式开始。您可以在
R.layout中使用特定的布局。您的_布局
您需要创建类的实例以使用实例方法。如果这有道理,那太好了!然而,如果这句话给你留下了问题,那么在你把自己挖进一个更大的洞之前,你应该复习一下:)你正在为这个对象做一个实例吗?比如myDB instanceVariable=new myDB?如果没有,请使用它,然后将instanceVariable用作instanceVariable.getStudents()。记住要公开,否则你无法访问我的坏消息,没有看到KeLiuyue的答案:)