【问题标题】:Android : get id of listview row when button clickedAndroid:单击按钮时获取列表视图行的ID
【发布时间】:2014-09-05 23:03:01
【问题描述】:

我有一个列表视图,其中包含由数据库填充的每个列表项的相对视图,并包含两个按钮。 当按下列表视图项中的一个按钮时,我需要在列表视图中获取该行的 id。换句话说,我试图找到与 onItemClickListener() 的 onItemClick 方法中的最后一个参数相同的东西,这将返回整个项目的点击。但是我只是在监听 listview 项目中的按钮,所以我无法使用 onItemClickListener()。

单击按钮时会调用此方法

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();       
    }

它需要找到我可以传递 ass arg 的相对布局的 id

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;
}

创建一个包含此列表视图项中数据的新光标

【问题讨论】:

  • 如何为每个按钮设置OnClickListener
  • "创建一个包含此列表视图项中数据的新游标"...嗯...为什么需要一个新游标?你不使用CursorAdapter吗?如果没有,我强烈建议你这样做。听起来您正在尝试在这里重新发明轮子。
  • 按钮当前设置为xml android:onClick="plusClick"
  • @sam:最简单的解决方案是使用适当的数据(即 id,如果您只对这些数据感兴趣)标记按钮。或者,自己设置OnClickListener。然后,您可以在即构造函数中将数据作为额外参数提供,或者将侦听器实现为匿名类,直接引用相关数据。

标签: java android listview android-listview


【解决方案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

        }
    };

【讨论】:

    【解决方案2】:

    当你为你的按钮设置 onclicklistener 时,你可以为它设置标签(标签可以是行的 id),然后当你点击按钮时得到它的标签。 (当然如果你使用自定义适配器)

    【讨论】:

      【解决方案3】:

      你需要某种适配器,在 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-27
        • 2013-12-30
        • 2021-12-15
        • 2012-06-24
        • 2013-09-12
        • 1970-01-01
        相关资源
        最近更新 更多