android - 获取 ListView 中所选按钮的位置

标签 android button android-listview onitemclicklistener

我有一个 ListView 列表中的每个项目都包含 TextView 和按钮我想要的是当我点击列表中任何项目的按钮时我想打印出按钮的位置如果我点击第一行的按钮我想打印出 0 并继续,但它不起作用 这是我显示 View 的方法

    private void displayListView() {

            Cursor cursor = dbHelper.fetchAllCountries();

            // The desired columns to be bound
            String[] columns = new String[] {
                    PhonesDbAdapter.KEY_NAME,
                    PhonesDbAdapter.KEY_CONTINENT,

            };

            // the XML defined views which the data will be bound to
            int[] to = new int[] {
                    R.id.continent,
                    R.id.name

            };

            // create the adapter using the cursor pointing to the desired data
            //as well as the layout information
            dataAdapter = new SimpleCursorAdapter(
                    this, R.layout.phone_layout,
                    cursor,
                    columns,
                    to,
                    0);

            listView = (ListView) findViewById(R.id.listView1);
            // Assign adapter to ListView
            listView.setAdapter(dataAdapter);
    }
    public void print(View v)
        {

        }


and here how my item look like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip"
    android:background="#f0f0f0" >

    <TextView
        android:id="@+id/continent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"

        android:textColor="#275a0d"/>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"

        android:text="TextView"
        android:textColor="#000"/>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/ic_launcher"/>

    <Button
        android:id="@+id/call"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/button1"
        android:background="@drawable/ic_launcher"
        android:onClick="print"/>

</RelativeLayout>

and this the layout that contain the listview
<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#f0f0f0">

        <EditText android:id="@+id/myFilter" android:layout_width="match_parent"
            android:layout_height="wrap_content" android:ems="10">
        </EditText>

        <ListView android:id="@+id/listView1" android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>

我花了很多时间解决这个问题,我希望你能帮助我,我真的很抱歉我的英语不好

最佳答案

您是否在您的适配器上实现方法 getView()? 这就是您要将 OnClickListener 添加到 Button

的地方

完成后,您可以将位置设置为 Button 的标记,并在 onClick 方法中检索它。

像这样:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.your_item_layout, parent, false);
        Button myButton = (Button) row.findViewById(R.id. call);
        myButton.setOnClickListener(new OnClickListener {

            @Override
            public void onClick(View view) {
                int position = (int)view.getTag();
                //Do whatever you want with the position here
            }
        });

        myButton.setTag(position);

        return row;
}

关于android - 获取 ListView 中所选按钮的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924274/

相关文章:

android - Nexus 7 编译时没有出现按钮

javascript - 更改 Button 元素的类以显示/隐藏它

android - ListView 项目的颜色非常淡

带有 ListView 的 Android 小部件无法正确加载项目

c# - 如何在单击事件中设置 Canvas ZIndex WPF 按钮控件?

滚动时android自定义 ListView 非常慢

android - 将 FragmentPagerAdapter 与 ArrayAdapter 一起使用时出现 IllegalStateException

android - 未找到设备 Flutter VSCode 模拟器

android - 如何通过Android中的耳机发送音频输出?

java - 在 Eclipse 中导入 ant build.xml