如何获取项目的位置点击列表视图?

时间:2011-09-16 17:57:32

标签: android listview

我的活动代码,用于获取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);
}

4 个答案:

答案 0 :(得分:4)

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

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) 

答案 1 :(得分:0)

对于ListActivity,您可以覆盖onListItemClick类中的方法ListActivity

答案 2 :(得分:0)

扩展ListActivity时,可以覆盖onListItemClick方法,如下所示:

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

答案 3 :(得分:0)

你应该实施

onListItemClick

并覆盖方法:

 @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);

    }