获取列表视图中所选按钮的位置

时间:2014-07-24 02:43:24

标签: android button android-listview onitemclicklistener

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

    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>

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

2 个答案:

答案 0 :(得分:2)

您是否在适配器上实现了方法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;
}

答案 1 :(得分:1)

从最近3年开始,我不在开发android,所以我不确定我的建议是否正确。你需要为你的目的进行测试。

在上述情况下,您不能使用onItemClickListner,因为它只能在跟踪完整列表行时使用。我认为你可以创建一个自定义适配器并覆盖其getView方法。在getView方法中,您将拥有自己的位置,并且可以在getview中为您设置按钮单击列表器。因此,通过这种方式,您可以获得按钮位置。

如果您需要一些代码,请尝试一次,然后我可以尝试为您编写代码......

这就是你如何创建自定义适应器。

public class my_custom_adapter extends ArrayAdapter<String> {
    private Context context                     = null;
    ArrayList<String>  elements                 = null;

    public my_custom_adapter(Context context, int type, ArrayList<String>  elements)
    {
        super(context, type, elements);
        this.elements =  elements;
        this.context = context;
    }


    //THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
    static class ViewHolder
    {
        public TextView tv;
        public Button cb;
    }

    @Override
    public View getView (final int position, View convertView, ViewGroup parent)
    {
        View rowView = convertView;
        ViewHolder holder = null;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                               Context.LAYOUT_INFLATER_SERVICE);
            // HERE I AM INFLATING LISTVIEW LAYOUT.
            rowView = inflater.inflate(R.layout.inflated_layout, null, false);
            holder = new ViewHolder();
            holder.cb = (Button) rowView.findViewById(R.id.checkBox1);
            holder.tv = (TextView) rowView.findViewById(R.id.textView1);
            rowView.setTag(holder);

        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        if (holder != null) {

            holder.cb.setOnClickListener(new View.OnClickListener() {

                @Override
            public void onClick(View arg0) {
                // IF YOUR BUTTON TAG HERE AND YOU CAN HAVE POSITION USING "position" PARAMETER

            }
            });
        }
        return rowView;
    }
}