2016-05-28 7 views
0

これは私のデータベースハンドラファイルです パッケージcom.example.saranshagarwal.studentdetailsapp;ダイアログボックスにsqliteデータを表示します。

public class MyDBHandler extends SQLiteOpenHelper { 
public static final int DATABASE_VERSION = 1; 
public static final String DATABASE_NAME = "mydetails.db"; 
public static final String TABLE_MYDETAIL = "mydetails"; 
public static final String COLUMN_NAME = "_name"; 
public static final String COLUMN_ROLLNUMBER = "_rollNumber"; 
public static final String COLUMN_MARKS = "_marks"; 
public static final String COLUMN_ID = "_id"; 

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory cursorFactory, int version) { 
    super(context, DATABASE_NAME, cursorFactory, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    String query = "CREATE TABLE " + TABLE_MYDETAIL + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_ROLLNUMBER + " INTEGER, " + COLUMN_NAME + " TEXT, " + COLUMN_MARKS + " INTEGER" + ");"; 
    Log.d("query", query); 
    db.execSQL(query); 

} 

public void addMyDetail(MyDetails myDetails) { 
    ContentValues values = new ContentValues(); 
    values.put(COLUMN_ROLLNUMBER, myDetails.get_rollNumber()); 
    values.put(COLUMN_NAME, myDetails.get_name()); 
    values.put(COLUMN_MARKS, myDetails.get_marks()); 
    SQLiteDatabase db = getReadableDatabase(); 
    long id = db.insert(TABLE_MYDETAIL, null, values); 
    Log.d("id", "addMyDetail: " + id); 
    db.close(); 
} 

public void deleteMyDetail(String myName) { 
    SQLiteDatabase db = getReadableDatabase(); 
    db.execSQL("DELETE FROM " + TABLE_MYDETAIL + " WHERE " + COLUMN_NAME + "=\"" + myName + "\";"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_MYDETAIL); 
    onCreate(db); 
} 

public String databaseToString() { 
    String dbString = ""; 
    SQLiteDatabase db = getWritableDatabase(); 
    String query = "SELECT * FROM " + TABLE_MYDETAIL; 
    Cursor c = db.rawQuery(query, null); 
    c.moveToFirst(); 
    while (!c.isAfterLast()) { 
     if (c.getString(c.getColumnIndex("_marks")) != null) { 
      dbString += c.getString(c.getColumnIndex(COLUMN_ROLLNUMBER)); 
      dbString += c.getString(c.getColumnIndex(COLUMN_NAME)); 
      dbString += c.getString(c.getColumnIndex(COLUMN_MARKS)); 
      dbString += "\n"; 
     } 
     c.moveToNext(); 
    } 
    c.close(); 
    db.close(); 
    return dbString; 
} 
} 

これは私のmainActivity パブリッククラスMainActivityがAppCompatActivity { のEditText名、rollnumber、マークを拡張しています。 MyDBHandler myDBHandler; TextView tv2; finalコンテキストコンテキスト= this;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    name = (EditText) findViewById(R.id.editText2); 
    rollnumber = (EditText) findViewById(R.id.editText5); 
    marks = (EditText) findViewById(R.id.editText4); 
    tv2 = (TextView) findViewById(R.id.textView5); 
    myDBHandler = new MyDBHandler(this, null, null, 1); 

} 

public void addButtonClicked(View view) { 
    MyDetails myDetails = new MyDetails(); 
    myDetails.set_rollNumber(rollnumber.getText().toString()); 

    myDetails.set_name(name.getText().toString()); 
    myDetails.set_marks(marks.getText().toString()); 
    myDBHandler.addMyDetail(myDetails); 
    String dbString = myDBHandler.databaseToString(); 
    tv2.setText(dbString); 
    /*name.setText(""); 
    rollnumber.setText(""); 
    marks.setText("");*/ 
} 

public void deleteButtonClicked(View view) { 
    String inputText = name.getText().toString(); 
    myDBHandler.deleteMyDetail(inputText); 

} 

public void onViewClicked(View view) { 
    Dialog dialog = new Dialog(context); 
    dialog.setContentView(R.layout.popup_on_view); 
    TextView tv1 = (TextView) findViewById(R.id.popup_text); 
    String dbString = myDBHandler.databaseToString(); 

    dialog.show(); 
    tv1.setText(dbString); 
} 

}

これは、ログ・ファイル

log

これはアプリケーションのスクリーンショットです。 enter image description here

1:私は、アプリのクラッシュが App

1を助けてくださいビュー]ボタンをクリックするとenter image description here

答えて

0
public void onViewClicked(View view) { 
    Dialog dialog = new Dialog(context); 
    dialog.setContentView(R.layout.popup_on_view); 

// TextViewにTV1 =(TextViewの)findViewById(R.id.popup_text );

// use this 
     TextView tv1 = (TextView) view.findViewById(R.id.popup_text); 

    String dbString = myDBHandler.databaseToString(); 

    dialog.show(); 
    tv1.setText(dbString); 
} 
関連する問題