2014-08-31 69 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 

     } 
    }; 
相关问题