如何获得项目的位置在列表视图点击? [英] How to get position of item clicked on listview?
本文介绍了如何获得项目的位置在列表视图点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的活动code,以获得ListActivity项目的位置更新状态检查数据库
公共类Viewlist产品扩展ListActivity {
私人ListViewAdapter lAdapter;
DBAdapter分贝;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); DB =新DBAdapter(本);
db.open();
光标光标= db.fetchAllDeliveryItem();
lAdapter =新ListViewAdapter(getApplicationContext(),光标);
//Toast.makeText(getApplicationContext(),cursor.getString(1),Toast.LENGTH_SHORT).show();
setListAdapter(lAdapter);}
私有类ListViewAdapter扩展的CursorAdapter {
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
// TODO自动生成方法存根 返回super.getView(位置,convertView,父母);
}公共ListViewAdapter(上下文的背景下,光标C){
超(背景下,C);
// TODO自动生成构造函数存根
}
@覆盖
公共无效bindView(查看视图,上下文的背景下,光标光标){
TextView的tvListText =(TextView中)view.findViewById(R.id.label);
复选框cbListCheck =(复选框)view.findViewById(R.id.check); tvListText.setText(cursor.getString(cursor.getColumnIndex(ITEMNAME)));
** cbListCheck.setChecked((cursor.getInt(cursor.getColumnIndex(交付))== 0假:真));
cbListCheck.setOnCheckedChangeListener(新OnCheckedChangeListener(){ @覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
db.updateItem(位置)**
}
});}@覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
LayoutInflater李=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
返回li.inflate(R.layout.receiverow,父母,假);
}
解决方案
您需要致电setOnItemClickListener您的ListView。它的回调函数包括被点击作为自变量的视图中的位置。下面是一个例子:
myListView.setOnItemClickListener(新AdapterView.OnItemClickListener(){ 公共无效onItemClick(适配器视图父母,视图V,INT位置,长的id){ // DO的东西在这里 }
});
修改:对不起,我以为你用一个ListView不是一个ListActivity。它即使是ListActivity容易,你只需实现你的ListActivity下面的方法:
onListItemClick(ListView中升,视图V,INT位置,长的ID)
my activity code to get position of item on ListActivity to update status checked to database
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);
}
解决方案
You need to call setOnItemClickListener on your ListView. Its callback function includes the position of the view that is clicked as a argument. Here is an example:
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id){
// DO STUFF HERE
}
});
Edit: Sorry I thought you were using a ListView not a ListActivity. Its even easier for a ListActivity as you simply implement the following method in your ListActivity:
onListItemClick (ListView l, View v, int position, long id)
这篇关于如何获得项目的位置在列表视图点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文