简体   繁体   English

如何获得在列表视图上单击的项目的位置?

[英]How to get position of item clicked on listview?

my activity code to get position of item on ListActivity to update status checked to database 我的活动代码以获取项目在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);
}

You need to call setOnItemClickListener on your ListView. 您需要在ListView上调用setOnItemClickListener。 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. 编辑 :对不起,我认为您使用的是ListView而不是ListActivity。 Its even easier for a ListActivity as you simply implement the following method in your ListActivity: 对于ListActivity,甚至更简单,因为您只需在ListActivity中实现以下方法:

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

在ListActivity的情况下,可以覆盖的方法onListItemClickListActivity类。

When you extend ListActivity you can override the onListItemClick method like this: 扩展ListActivity时,可以像这样重写onListItemClick方法:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Do stuff
}

you should implement 你应该实施

onListItemClick onListItemClick

and override the method : 并重写方法:

 @Override
    protected void onListItemClick(ListView l, View v, final int position, long id) {
        super.onListItemClick(l, v, position, id);  

Log.i("the Item clicked is at position : ",  position);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM