2012-02-29 22 views
0

這是我的代碼,其中從數據庫中提取值a顯示它們在各自的字段中我設計了一個自定義列表,其中有5個textview和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); 

在ListView每行包括這些元素 屏幕看起來像下面 TextView1 Textview2 Textview3 Textview4 Textview5 Button1的Button2的BUTTON3

+1

見本的若干現有職位之一: http://stackoverflow.com/questions/8563915/android-get-row-position-from-列表查看/ 8564251#8564251 – jsmith 2012-02-29 14:08:04

+0

請讓我知道答案 – Girish 2012-03-01 10:26:39

回答

0

如果我沒有誤解你必須實現

onListItemClick作爲參數

的位置來看在列表中的位置

那麼您可以在

youradapter.getItem(position) 

看到該文檔在: getItem(int)

+0

即時通訊每行有5個textview和3個按鈕像Textview1 – Girish 2012-02-29 14:20:10

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*/ 


     } 

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

     } 
    }); 

,其中位置是行號r

+0

我希望你沒有得到我的問題 – Girish 2012-03-01 09:58:09

0

爲了從列表項中捕獲事件,您將不得不創建一個自定義適配器。在適配器中,您將自己填充控件中的數據。您也可以使用這些控件註冊事件。要告訴控件來自哪一行,您需要使用行號或遊標值設置控件上的標籤。當事件被觸發時,您可以從控制中取回。

以下是自定義適配器的示例。它可能指向你在正確的方向:

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)); 
} 
} 
+0

我希望每個按鈕可連續點擊 – Girish 2012-03-05 07:58:56

+0

plz發送完整碼 – Girish 2012-03-05 09:47:05

相關問題