这是我的databasehandler文件 包com.example.saranshagarwal.studentdetailsapp;
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扩展AppCompatActivity { EditText名称,rollnumber,marks; MyDBHandler myDBHandler; TextView tv2; 最终的Context context = 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);
}
}
这是日志文件
这是该应用的屏幕截图。当我点击查看按钮时,应用程序崩溃请帮忙 App
1:
1:
答案 0 :(得分: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);
}