这是我的代码,我从数据库中获取值a在各自的字段中显示它我设计了一个自定义列表,其中我有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
答案 0 :(得分:1)
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
其中position是行号
答案 1 :(得分:0)
如果我没有误解你必须实施
onListItemClick具有参数
position视图在列表中的位置
然后你可以
youradapter.getItem(position)
请参阅以下文档: getItem(int)
答案 2 :(得分:0)
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*/
}
});
答案 3 :(得分:0)
为了从List项中捕获事件,您将不得不创建自定义适配器。在适配器中,您将自己填充控件中的数据。您还可以注册具有这些控件的事件。要告诉控件来自哪一行,您需要使用行#或游标值在控件上设置标记。然后,您可以在触发事件时从控件中获取该信息。
以下是自定义适配器的示例。它可能会指出你正确的方向:
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));
}
}