gpt4 book ai didi

java - 如何从 ListView 的每一行中获取项目?

转载 作者:行者123 更新时间:2023-11-29 20:58:38 25 4
gpt4 key购买 nike

我想获取listView中被点击行的数据。

这是我的代码,我想访问每行 ListView 中的“ID”值。

如何从 ListView 的每一行中获取项目?

如何从 ListView 的每一行中获取项目?

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;
}
}

}

最佳答案

你应该实现 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


}
});

关于java - 如何从 ListView 的每一行中获取项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26853775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com