I’ve created this code that populate SQLite in Listview, but what I’m struggeling in is that I want to show TOAST message depending on what user chooses from the listview.Should I go with onClick method?.
public void createTable(SQLiteDatabase mDb, String table) {
try {
mDb.execSQL("create table if not exists "
+ table
+ " (id integer primary key autoincrement, "
+ "username text not null, birthday text not null,image text);");
} catch (SQLException e) {
Toast.makeText(getApplicationContext(), "yes",
Toast.LENGTH_LONG).show();
}
}
public void insert(SQLiteDatabase mDb, String table) {
ContentValues values = new ContentValues();
public void getAllData(String table) {
Cursor c = mDb.rawQuery("select * from " + table, null);
int columnsSize = c.getColumnCount();
listData = new ArrayList<HashMap<String, Object>>();
while (c.moveToNext()) {
HashMap<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < columnsSize; i++) {
map.put("id", c.getString(0));
map.put("username", c.getString(1));
map.put("birthday", c.getString(2));
map.put("image", c.getString(3));
}
listData.add(map);
}
}
public boolean delete(SQLiteDatabase mDb, String table, int id) {
String whereClause = "id=?";
String[] whereArgs = new String[] { String.valueOf(id) };
try {
mDb.delete(table, whereClause, whereArgs);
} catch (SQLException e) {
Toast.makeText(getApplicationContext(), "هˆ 除و•°وچ®ه؛“ه¤±è´¥",
Toast.LENGTH_LONG).show();
return false;
}
return true;
}
}
OnCreateContextMenuListener listviewLongPress = new OnCreateContextMenuListener(){
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
final AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) menuInfo;
new AlertDialog.Builder(ListView_SqliteActivity.this)
.setTitle("عنوان1")
.setIcon(android.R.drawable.ic_dialog_info)
.setMessage("عنوان 2")
.setPositiveButton("وک¯",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
int mListPos = info.position;
HashMap<String, Object> map = listData
.get(mListPos);
int id = Integer.valueOf((map.get("id")
.toString()));
if (dao.delete(mDb, "student", id)) {
listData.remove(mListPos);
listItemAdapter.notifyDataSetChanged();
}
}
})
.setNegativeButton("هگ¦",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
}
}).show();
}
};
@Override
public void finish() {
// TODO Auto-generated method stub
super.finish();
mDb.close();
}
}
I assume little from your question that you want to implement Click event for the ListView, and you want to display Toast Message for the click.
So, implement
setOnItemClickListenerfor your listview to handle list item click.