2014-08-31 106 views
0

我有一個列表視圖,每個列表項包含由數據庫填充幷包含兩個按鈕的相關視圖。 當列表視圖項中的按鈕之一被按下時,我需要在列表視圖中獲得該行的ID。換句話說,我試圖找到onItemClickListener()的onItemClick方法中的最後一個參數,它會返回整個項目的點擊。不過,我只是在ListView項目中聽按鈕,所以我無法使用onItemClickListener()。Android:得到按鈕點擊時的列表視圖行的ID

當我按一下按鈕

public void plusClick(View v) 
    { 

     //get the row the clicked button is in 
     RelativeLayout vwParentRow = (RelativeLayout)v.getParent(); 

     //i need to get the id of this RelativeLayout item in the list view 

     TextView child = (TextView)vwParentRow.getChildAt(2); 
     Button btnChild = (Button)vwParentRow.getChildAt(1); 


     int c = Color.CYAN; 


     vwParentRow.setBackgroundColor(c); 
     vwParentRow.refreshDrawableState();  
    } 

我已經被調用該方法,需要找到相對佈局,我可以通過屁股ARG爲

public Cursor getRow(long rowId) { 
    String where = KEY_ROWID + "=" + rowId; 
    Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, 
        where, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
} 

創建一個新的ID光標contiain數據在此列表視圖項

+0

如何爲每個按鈕設置「OnClickListener」? – 2014-08-31 08:21:50

+0

「創建一個新的遊標,在此列表視圖項目中添加數據」... umm ...爲什麼需要新的遊標?你不使用'CursorAdapter'嗎?如果不是,我強烈建議你這樣做。聽起來你正試圖在這裏重新發明輪子。 – 2014-08-31 08:27:02

+0

按鈕當前設置與XML的android:的onClick =「plusClick」 – sam 2014-08-31 08:30:37

回答

0

當你設置onclicklistener你的按鈕,你可以將它設置標籤(標籤可以是行的ID),然後當你點擊按鈕,得到它的標籤。 (當然,如果你使用自定義適配器)

0

你需要某種形式的適配器,在getView方法,你必須聲明你的活動

有一個例子:

holder.hMention.setOnClickListener(new VerTweet(mention)); 

和...

public class VerTweet implements View.OnClickListener 
{ 

    Mention mention; 

    public VerTweet (Mention mention) { 
     this.mention = mention; 
    } 

    public void onClick(View view){ 

     Bundle bundle = new Bundle(); 
     //Le ponemos la variable parametro con el contenido test 
     bundle.putInt("idTrack", mention.getIdTrack()); 
     bundle.putString("Gifo", mention.getGifo()); 
     bundle.putString("From", mention.getFromUser()); 
     bundle.putString("Date", mention.getDateMentions()); 
     bundle.putString("Text", mention.getText()); 
     bundle.putLong("Status", mention.getLastId()); 
     Intent i = new Intent(getContext(), Tweet.class); 
     i.putExtras(bundle); 
     getContext().startActivity(i); 

    } 
} 

https://github.com/m-alcu/SkoltiAlert/blob/master/src/com/alcubier/skoltialert/mentions/GDMentionsList.java

1

試試這個代碼:

private OnItemClickListener itemClickListener = new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> av, View v, int position, 
       long id) { 
       // write your logic here   
       // position starts from 0 
       // id starts from 1 

     } 
    }; 
相關問題