[英]How do I read certain data from sqlite database and show it in a dialog when a listview item is clicked?
This shows my listview onclick listener and I would like to open a dialog when one item is clicked and it to show sqlite information.How do I get the date and the note when clicked on the listview item. 这显示了我的listview onclick侦听器,当我单击一个项目时,我想打开一个对话框以显示sqlite信息。单击listview项时如何获取日期和注释。 The following is my attempt but it does not work.
以下是我的尝试,但不起作用。 Also i am fairly new to android programming so if you can sort the problem out in the code that would be very helpful for me.
我也是android编程的新手,所以如果您可以在代码中解决问题,那对我非常有帮助。
ListAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
//set an onItemClickListener to the ListView
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
Log.d(TAG, "onItemClick: You Clicked on " + name);
Cursor data = mDatabaseHelper.getexpenseItemID(name); //get the id associated with that name
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
displayNoteDate(mDatabaseHelper.getexpenseNote(data2),mDatabaseHelper.getexpenseDate(data1));
}
else{
toastMessage("No ID associated with that name");
}
}
});
public void displayNoteDate(String noteContent, String dateValue) {
final Dialog builder = new Dialog(getActivity());
builder.setContentView(R.layout.custom_dialog);
builder.setTitle("Display note date");
TextView note = (TextView)builder.findViewById(R.id.note);
TextView date = (TextView)builder.findViewById(R.id.date);
//add the database note and date
note.setText(noteContent);
date.setText(dateValue);
Button closeButton = (Button)builder.findViewById(R.id.close);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder.dismiss();
}
});
builder.show();
}
DatabaseHelper.java DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
public static final String DATABASE_NAME = "budget.db";
public static final String TABLE_NAME = "expense_table";
public static final String TABLE_NAME2 = "income_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "ID2";
public static final String EXPENSE_AMOUNT = "AMOUNT";
public static final String EXPENSE_DATE = "DATE";
public static final String EXPENSE_NOTES = "NOTES";
public static final String INCOME_AMOUNT = "AMOUNT";
public static final String INCOME_DATE = "DATE";
public static final String INCOME_NOTES = "NOTES";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 3);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
db.execSQL("create table " + TABLE_NAME2 + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
onCreate(db);
}
public boolean insertexpenseData(String amount_expense, String date_expense, String notes_expense) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(EXPENSE_AMOUNT, amount_expense);
contentValues.put(EXPENSE_DATE, date_expense);
contentValues.put(EXPENSE_NOTES, notes_expense);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public boolean insertincomeData(String amount_income, String date_income, String notes_income) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INCOME_AMOUNT, amount_income);
contentValues.put(INCOME_DATE, date_income);
contentValues.put(INCOME_NOTES, notes_income);
long result = db.insert(TABLE_NAME2, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public Cursor getexpenseData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
public Cursor getincomeData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME2, null);
return res;
}
public Cursor getexpenseDate(Cursor date){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + EXPENSE_DATE + " = '" + date + "'";
Cursor data1 = db.rawQuery(query, null);
return data1;
}
public Cursor getexpenseNote(Cursor note){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + EXPENSE_NOTES + " = '" + note + "'";
Cursor data2 = db.rawQuery(query, null);
return data2;
}
public Cursor getexpenseItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL_1 + " FROM " + TABLE_NAME + " WHERE " + EXPENSE_AMOUNT + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getincomeItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL_2 + " FROM " + TABLE_NAME2 + " WHERE " + INCOME_AMOUNT + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public boolean updateexpenseData(String id, String amount, String date, String notes) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(EXPENSE_AMOUNT, amount);
contentValues.put(EXPENSE_DATE, date);
contentValues.put(EXPENSE_NOTES, notes);
db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});
return true;
}
public boolean updateincomeData(String id, String amount, String date, String notes) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, id);
contentValues.put(INCOME_AMOUNT, amount);
contentValues.put(INCOME_DATE, date);
contentValues.put(INCOME_NOTES, notes);
db.update(TABLE_NAME2, contentValues, "ID = ?", new String[]{id});
return true;
}
public Integer deleteexpenseData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?", new String[]{id});
}
public Integer deleteincomeData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME2, "ID = ?", new String[]{id});
}
}
Use this method 使用这个方法
Cursor cursor = mDatabaseHelper.getexpenseItemID(name);
if (cursor.getCount() > 0){
cursor.moveToFirst();
String noteContent = cursor.getString(cursor.getColumnIndex("NOTES"));
String date = cursor.getString(cursor.getColumnIndex("DATE"));
Toast.makeText(Activity.this, noteContent, Toast.LENGTH_SHORT).show();
Toast.makeText(Activity.this, date, Toast.LENGTH_SHORT).show();
displayNoteDate(noteContent, date);
}
As long as the toast echoes your data, know you've successfully read it from your database. 只要吐司回显了您的数据,就知道您已成功从数据库中读取数据。
Here's a solution (assuming that the displayNoteDate
works, this was tested with a Toast), that uses a CursorAdapter rather than a ListAdapter . 这是一个解决方案(假设
displayNoteDate
可以工作,并且已经通过Toast进行了测试),该解决方案使用CursorAdapter而不是ListAdapter 。
The benefit is two-fold, no need to create an intermediate array and you have ALL the data available within the onItemClickListener
. 好处是双重的,不需要创建中间数组,并且您在
onItemClickListener
拥有所有可用数据。
One pitfall though is that a Cursor adpater expects a column named _id , as such I changed the onCreate
method in the helper to be :- 但是,一个陷阱是Cursor adpater希望使用名为_id的列,因此我将帮助器中的
onCreate
方法更改为:-
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
db.execSQL("create table " + TABLE_NAME2 + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,AMOUNT INTEGER,DATE INTEGER,NOTES TEXT)");
}
ie _id replacing ID 即_id替换ID
For testing purposes I included the following to add some data :- 为了进行测试,我添加了以下内容以添加一些数据:
mDatabaseHelper.insertexpenseData("100.00","21/03/1904","Wow this old?");
mDatabaseHelper.insertexpenseData("56.78","31/12/2010","New Celebrations");
mDatabaseHelper.insertexpenseData("250.00","23/08/2017","Birthday Present");
The actual code I used (which other than the first line to get the data as a cursor) closely relates to yours (except the body of onItemClick
, which gets the data from the cursor)) was:- 我使用的实际代码(除了第一行以外的其他数据将其作为游标获取)与您的代码紧密相关(
onItemClick
的主体( onItemClick
从游标获取数据)除外)是:
.......
expensedata = mDatabaseHelper.getexpenseData();
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
expensedata,
new String[]{DatabaseHelper.EXPENSE_AMOUNT},
new int[]{android.R.id.text1}, 0);
listdata.setAdapter(sca);
listdata.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int csrpos = expensedata.getPosition();
expensedata.moveToPosition(i);
displayNoteDate(
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_NOTES)),
expensedata.getString(expensedata.getColumnIndex(DatabaseHelper.EXPENSE_DATE)));
expensedata.moveToPosition(csrpos);
}
});
}
//!!!NOTE!!! Used for testing so don't use this method.
private void displayNoteDate(String notes, String date) {
Toast.makeText(this,"Notes=" + notes + " Incurred on " + date,Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
expensedata.close();
}
Note expensedata
was declared as a class variable using Cursor expensedata;
注意
expensedata
被宣布为使用类变量Cursor expensedata;
Note you should close the cursor when finished with it. 请注意,完成操作后应关闭光标。 I used an activity and thus used the
onDestroy
method for this. 我使用了一个活动,因此为此使用了
onDestroy
方法。
When run it displays :- 运行时显示:-
When 56.78 is clicked (likewise for the other List items) then :- 当单击56.78 (对于其他列表项也是如此)时:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.