[英]How to differentiate between buttons in a ListView
I want to make a list of connections and give user a option to connect to any one of them. 我想列出一个连接,并为用户提供连接到其中任何一个的选项。
<TextView
android:id="@+id/tvName"
android:layout_width="225dp"
android:layout_height="38dp"
android:textSize="25sp" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="connect"
android:text="connect" />
This is the basic structure of each item in the list. 这是列表中每个项目的基本结构。 After creating the list, in "connect" function, I am not able not figure out which button in the list called it.
创建列表后,在“连接”功能中,我无法确定列表中的哪个按钮调用了它。 I have to know that to get connected to that particular connection.
我必须知道要连接到该特定连接。 Can anyone please help me out to know the position of the button clicked?
谁能帮助我知道单击按钮的位置吗? Thank you in advance.
先感谢您。
You can set the onClick event in your custom adapter's getView method. 您可以在自定义适配器的getView方法中设置onClick事件。
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vehicals_details_row, parent,
false);
Button btnView = (Button) row.findViewById(R.id.button1);
btnView .setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
}
});
For more info Check the link Handling Button clicks in a ListView Row 有关更多信息,请检查ListView行中的链接Handling Button单击。
Add an OnItemClickListener
to the ListView
using setOnItemClickListener
. 使用
setOnItemClickListener
将OnItemClickListener
添加到ListView
中。 The listener has the following callback function: 侦听器具有以下回调函数:
onItemClick(AdapterView<?> parent, View view, int position, long id)
( position
is what you want, i guess) (我想
position
就是你想要的)
You have basically two choices: 您基本上有两个选择:
1) In the adapter, attach to every button (in the view) a listener, referring to the correct item in your list (I assume the items are instances of the (invented) class ConnectionItem): 1)在适配器中,将侦听器附加到视图中每个按钮(在视图中),以引用列表中的正确项(我假设这些项是(发明)类ConnectionItem的实例):
View itemView = .... //view inflated here
ConnectionItem connItem = ..//the current connection
Button button = (Button) itemView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Log.i(YOUR_TAG, "clicked on item:"+connectionItem.getName());
}
});
2) In the adapter, you set a tag to the view with the item itself: 2)在适配器中,使用项目本身为视图设置标签:
View itemView = .... //view inflated here
ConnectionItem connItem = ..//the current connection
Button button = (Button) itemView.findViewById(R.id.button1);
button.setTag(connItem)
Then in your activity: 然后在您的活动中:
public void connect(View view) {
ConnectionItem connItem = (ConnectionItem)view.getTag();
Log.i(YOUR_TAG, "clicked on item:"+connectionItem.getName());
}
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.