我已经创建了这个在Listview中填充SQLite的代码,但是我想要的是我想根据用户从listview中选择的内容来显示TOAST消息。我应该使用onClick方法吗?。
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();
}
}
答案 0 :(得分:0)
我假设您想要为ListView实现Click事件,并且您希望显示点击的Toast消息。
因此,为列表视图实施 setOnItemClickListener
以处理列表项单击。
list.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// do whatever you want
}
});