我想在listView中点击的行中获取数据。
这是我的代码,我想在每行od列表视图中访问“ID”值。
如何从列表视图的每一行获取该项目?
如何从列表视图的每一行获取该项目?
public class ActivityBookMark extends Activity {
private SQLiteDatabase database;
private ListView lv;
private int ID;
public ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_mark);
lv = (ListView) findViewById(R.id.BookMarkListView);
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.translate);
lv.setLayoutAnimation(new LayoutAnimationController(animation));
ArrayList<String> data = geData();
adapter = new ArrayAdapter<String>(this, R.layout.bookmark_item,
R.id.TXTbookmarkItem, data);
lv.setAdapter(adapter);
}
public ArrayList<String> geData() {
// TODO Auto-generated method stub
database = SQLiteDatabase.openDatabase(DBHelper.DB_PATH
+ DBHelper.DB_NAME, null, 0);
Cursor localCursor = database
.rawQuery(
"SELECT `rowid`,* FROM `TABLE1` WHERE 1=1 AND `FAV` LIKE '%TRUE%' ORDER BY `rowid` ASC LIMIT 0, 50000",
null);
ArrayList<String> result = new ArrayList<String>();
int TITLE = localCursor.getColumnIndex("TITLE");
//↓ i want to access ID value onto OnClickUI method
ID = localCursor.getColumnIndex("ID");
for (localCursor.moveToFirst(); !localCursor.isAfterLast(); localCursor
.moveToNext()) {
result.add(localCursor.getString(TITLE));
}
return result;
}
public void OnClickUI(View v) {
switch (v.getId()) {
case R.id.DelFromBookMark: {
//Code to retrive ID of each Row
.... .
Toast.makeText(ActivityBookMark.this,v.getViewTreeObserver()+"", Toast.LENGTH_SHORT).show();
database.execSQL("UPDATE TABLE1 SET FAV='FALSE' WHERE ID=" + ID
+ "");
List<String> newItems = geData();
adapter.clear();
adapter.addAll(newItems);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),
"از لیست نشان شده ها حذف شد!", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
答案 0 :(得分:2)
您应该实现onItemclicklistner
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
//in this method you get position and view of the item clicked the list
}
});