android – 在按钮单击时获取listview项目位置
作者:互联网
这是我的listview点击事件:
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails)o;
Toast.makeText(ListViewImagesActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
}
});
这是我的按钮点击事件:
btnNxt = (Button) findViewById(R.id.btnNext);
btnNxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Here I need to get that position
});
现在我需要获得点击的listview项目的位置..我已经全局声明了postion但总是给我位置1.
谁能说我怎么得到这个位置?
解决方法:
你执行这个吗?
btnNxt = (Button) findViewById(R.id.btnNext);
btnNxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Here I need to get that position
});
在getView方法中?
如果是这样的话很容易
btnNxt = (Button) findViewById(R.id.btnNext);
btnNxt.setTag(position);
btnNxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int position=(Integer)arg0.getTag();
});
标签:buttonclick,listitem,android,android-listview,android-button 来源: https://codeday.me/bug/20190917/1809929.html