2017-10-27 12 views
-3

現在、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()コールの後に置くように修正されています。

編集:インスタンス変数の作成が機能しました。すべての助けに感謝します。

+0

"私はそれを静的にすると静的である必要があることを私に知らせるエラーが出ます"私はそれを疑う。 –

+2

インスタンスメソッドを使用するには、クラスのインスタンスを作成する必要があります。それが理にかなっていれば、素晴らしい!しかし、その文があなたに質問を残す場合は、[Javaのオブジェクト指向の概念](https://docs.oracle.com/)を確認する必要があります。com/javase/tutorial/java/concepts /)より大きい穴に自分自身を掘る前に:) – Jon

+0

そのオブジェクトのインスタンスをやっている? myDBのようにinstanceVariable = new myDB?そうでない場合は、それを使用し、instanceVariableをinstanceVariable.getStudents()として使用します。 – jace

答えて

1

クラス内では、クラス名を使用してstaticpublicのメソッドにアクセスすることはできません。

  • だからmyDB mMyDB;を使用してgetStudentsメソッドにアクセスするためにClass objectを使用することができます。

  • そして、あなたはあなたの方法static

そして、あなたのmyDB。あなたは、これに変更することができますで行うことができます。

public myDB(Context contextr) { 
    super(context, NAME , null , DB_VERSION , mull); 
} 

MainActivityにあります。

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.your_layoutを変更する必要があります。

そして、あなたのクラス名を変更し、この書き込みは非常に標準化されていません。

サンプルようmydbという

最初の文字のクラス名は大文字でなければなりません。

+0

それを試したが、それは違いはありませんでした。それでも同じ問題が発生しています。 –

+0

これは悪い答えではありませんが、オブジェクトにインスタンス化**することをOPに説明することができれば、それは役に立ちます。 – Jon

+0

ありがとう、それはそれを修正しました。私はまだエラーが出ていますが、これはR.layout。*の原因で、2つのエラーが発生します。予期せぬ不正な式の開始です。 –