gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:48:14 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com