【问题标题】:getting position of click and custom button inside a list using custom CursorAdapter使用自定义 CursorAdapter 在列表中获取单击和自定义按钮的位置
【发布时间】:2014-04-23 06:53:23
【问题描述】:

场景 朋友们,我有一个 ListView,我在 FrameLayout 中使用它,它将为我的 DrawerLayout 提供滑动菜单的动力。我的 ListView 中有自定义按钮和文本,它们在单独的 xml 中定义为列表项。现在我使用扩展 CursorAdapter 的 customAdapter 向这个 ListView 提供数据...

我想获取点击的位置以及按钮点击事件以相应地获取数据...我的代码如下...

列表项 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:descendantFocusability="blocksDescendants"
    >


     <!--  ToggleButton
        android:id="@+id/button"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_margin="10dp"
        style="@style/toggleButton"
        android:background="@drawable/silent_button_selector"
        android:focusable="false"
         /> --> 

    <ImageButton
        android:id="@+id/button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:src="@drawable/icon_button"
        />


    <TextView 
        android:id="@+id/calendarName"
        android:layout_width="wrap_content"
        android:layout_margin="10dp"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:layout_toRightOf="@+id/button"
        />


</RelativeLayout>

适配器类:

public class CalendarListAdapter extends CursorAdapter implements OnClickListener{

    @SuppressWarnings("deprecation")
    public CalendarListAdapter(Context context, Cursor c) {
        super(context, c);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
        TextView title = (TextView) view.findViewById(R.id.calendarName);
        System.out.println("cursor details"
                + cursor.getString(cursor.getColumnIndex(cursor
                        .getColumnName(1))));
        title.setText(cursor.getString(cursor.getColumnIndex(cursor
                .getColumnName(1))));
        Log.d("click position " , "position is " +cursor.getPosition() );
        ImageButton button = (ImageButton) view.findViewById(R.id.button);
        button.setOnClickListener(this);
        Log.d("click position " , "position is " +cursor.getPosition() );
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);

        View calendarListView = inflater.inflate(R.layout.calendar_list_item, null);

        return calendarListView;
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        Log.d("item clicked", "clicked" );

    }

}

现在发生了什么:

通过上述实现,当我单击按钮时,它只给出按钮单击事件,当我单击行/列表项时,我单独获得位置。

我需要做什么?

我需要同时获取位置和点击事件,所以当我点击第三行的按钮时,我需要位置 3 和按钮点击事件...

请告诉我最好的方法...感谢您的时间和努力..

【问题讨论】:

    标签: android android-listview android-cursoradapter


    【解决方案1】:

    处理场景的简单方法是利用按钮标签

    在您的 bindView 中

    ImageButton button = (ImageButton) view.findViewById(R.id.button);
    button.setTag(cursor.getPosition());
    

    然后在你的 onClick 中你可以读取标签

    int position = (Integer) view.getTag();
    

    【讨论】:

    • 谢谢让我试试,会告诉你.. 但是我现在已经将 ImageButtons 更改为 ToggleButtons.. 让我试试这个...@William Newby
    【解决方案2】:

    给数据库中的用户使用....

    在您的适配器中实现您的 Onclicklistener..

    在您的绑定视图中...

    ImageButton button = (ImageButton) view.findViewById(R.id.button);
    button.setTag(cursor.getint("number colunm _id here!!");
    

    在您的点击中...

    int position = (Integer) view.getTag();
    

    再见

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多