繁体   English   中英

单击列表中的按钮时如何在列表视图中获取行位置

[英]How to get the row position in a listview when a button is clicked inside it

这是我的代码,我从数据库中获取值并将它们显示在各自的字段中,我设计了一个自定义列表,其中有5个textviews和3个按钮。我的问题是如何使这些按钮可单击,我希望在下一个活动中获得该行信息。

cursor = db.rawQuery("SELECT * FROM JOB_LIST_DISPLAY_TABLE",null);

    adapter = new SimpleCursorAdapter(this,R.layout.customlist,cursor, 
            new String[] {"JOB_TITLE","JOB_START_DATE","JOB_END_DATE","JOB_STATE","JOB_SPECIALITY","JOBPERMANENT",}, 
            new int[] {R.id.Title,R.id.StartDate,R.id.EndDate,R.id.State,R.id.Speciality,R.id.JobType});
            listview.setAdapter(adapter);

列表视图中的每一行都包含这些元素。屏幕如下所示:TextView1 Textview2 Textview3 Textview4 Textview5 Button1 Button2 button3

list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        }
    });

位置是行号

如果我没有误会你就必须执行

作为参数的onListItemClick

position列表中视图的位置

那你可以

youradapter.getItem(position)

请参阅以下文档: getItem(int)

listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

              Log.d("list item position","="+position);

              /* If you want this position in  next activity  then put it in  bundle extra and start the activity,ten fetch it from bundle in the next activity*/


        }

    });

为了捕获列表项中的事件,您将必须创建一个自定义适配器。 在适配器内,您将自己填充控件中的数据。 您也可以使用这些控件注册事件。 要知道控件来自哪一行,您需要使用行号或光标值在控件上设置标记。 然后,您可以在触发事件时从控件取回该信息。

这是一个自定义适配器的示例。 它可能会指出正确的方向:

public class MyAdapter extends ResourceCursorAdapter {

private static final class ViewHolder { 
    public TextView mControl1;
    public TextView mControl2;
} 

private int mData1Col;
private int mData2Col;

public MyAdapter(Context context, Cursor cursor) {
    super(context, R.layout.history_entry, cursor, true);

    // Store cursor column indexes for efficiency. 
    if ( null != cursor ) {
        mData1Col   = cursor.getColumnIndex(DATA1);
        mData2Col   = cursor.getColumnIndex(DATA2);
    }
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    // This method creates new views as needed.  Most controls only create 
    // views they need to fill the visible display area, then they re-use them.

    // Let the parent create the view we specified at construction.
    View view = super.newView(context, cursor, parent);

    // For efficiency, use a view holder to reference the child views.
    // These find operations can be expensive so do it just once.
    ViewHolder vh = new ViewHolder();
    vh.mTitle   = (TextView) view.findViewById(R.id.control1);
    vh.mAt      = (TextView) view.findViewById(R.id.control2);
    view.setTag(vh);

    return (view);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    // This methods binds the specified cursor data with the provided view.

    // Use the ViewHolder to find the controls we need and populate them.
    ViewHolder vh = (ViewHolder) view.getTag();

            // Populate the controls with the current cursor.
            // Register to receive events from the controls.
            // Set the tag on your controls with the cursor position so you
            // have that info when the item is selected.
}

@Override
public Cursor swapCursor(Cursor newCursor) {

    // Store column indexes for efficiency. 
    if (null != newCursor) {
        mData1Col = newCursor.getColumnIndex(DATA1);
        mData2Col = newCursor.getColumnIndex(DATA2);
    } else {
        mTitleCol    = 0;
        mResolvedAtCol   = 0;
    }

    return (super.swapCursor(newCursor));
}
}

暂无
暂无

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

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