現在、ID、NAME、STATUSの3つの要素を持つデータベースを作成するプロジェクトを作成しようとしています。次に、これらの値をすべて同じアクティビティにある3つのリストビューに保存する必要があります。ここに私のsqliteのコードは次のとおりです。静的メソッド宣言のAndroidスタジオの問題
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()コールの後に置くように修正されています。
編集:インスタンス変数の作成が機能しました。すべての助けに感謝します。
"私はそれを静的にすると静的である必要があることを私に知らせるエラーが出ます"私はそれを疑う。 –
インスタンスメソッドを使用するには、クラスのインスタンスを作成する必要があります。それが理にかなっていれば、素晴らしい!しかし、その文があなたに質問を残す場合は、[Javaのオブジェクト指向の概念](https://docs.oracle.com/)を確認する必要があります。com/javase/tutorial/java/concepts /)より大きい穴に自分自身を掘る前に:) – Jon
そのオブジェクトのインスタンスをやっている? myDBのようにinstanceVariable = new myDB?そうでない場合は、それを使用し、instanceVariableをinstanceVariable.getStudents()として使用します。 – jace