gpt4 book ai didi

android - 如何获取在 ListView 上单击的项目的位置?

转载 作者:行者123 更新时间:2023-11-29 18:17:18 26 4
gpt4 key购买 nike

我的 Activity 代码用于获取项目在 ListActivity 上的位置以更新状态检查到数据库

public class ViewList extends ListActivity {
private ListViewAdapter lAdapter;
DBAdapter db;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

db = new DBAdapter(this);
db.open();
Cursor cursor = db.fetchAllDeliveryItem();
lAdapter = new ListViewAdapter(getApplicationContext(),cursor);
//Toast.makeText(getApplicationContext(), cursor.getString(1), Toast.LENGTH_SHORT).show();
setListAdapter(lAdapter);

}
private class ListViewAdapter extends CursorAdapter{


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

return super.getView(position, convertView, parent);
}

public ListViewAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}




@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tvListText = (TextView)view.findViewById(R.id.label);
CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.check);

tvListText.setText(cursor.getString(cursor.getColumnIndex("itemname")));
**cbListCheck.setChecked((cursor.getInt(cursor.getColumnIndex("delivered"))==0? false:true));
cbListCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
db.updateItem(position);**


}
});

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.receiverow, parent, false);
}

最佳答案

您需要在 ListView 上调用 setOnItemClickListener。它的回调函数包括作为参数单击的 View 的位置。这是一个例子:

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView parent, View v, int position, long id){

// DO STUFF HERE

}
});

编辑:抱歉,我认为您使用的是 ListView 而不是 ListActivity。对于 ListActivity 来说更容易,因为您只需在 ListActivity 中实现以下方法:

onListItemClick (ListView l, View v, int position, long id) 

关于android - 如何获取在 ListView 上单击的项目的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7448585/

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