在我的活动中单击自定义数组适配器的传播按钮
我的活动有一个 ListView,它有一个自定义 ArrayAdapter。 在我的 ArrayAdapter 上,我有一个图像、一堆文本框和一个按钮。
在适配器的 getView 上,我获取按钮并设置 setOnClickListener。从点击侦听器我可以获取点击项目的索引。
现在我的问题是我想将该信息传播到我的主要活动,我想在其中处理按钮单击。 我可以将索引信息保存在静态变量中,但我仍然不知道如何在我的活动中触发事件。
我该怎么做?
我刚接触 Android 6 天,谢谢 iggy
代码:
我的活动:
public class MyClass extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
...
myListView = (ListView)findViewById(R.id.lvxml);
myList = new MyCustomArrayAdapter(this, myAnotherClassObject);
myListView .setAdapter(myList);
...
}
}
现在在我的适配器中,
public class MyCustomArrayAdapter extends ArrayAdapter<myAnotherClass> {
...
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
doStuff;
}
});
}
}
我需要以某种方式从我的主要活动中触发按钮单击,但又不失去读取列表视图中单击的索引的可能性。
My activity has a ListView that has a custom ArrayAdapter.
On my ArrayAdapter i have an image, a bunch of textboxes and a button.
On the getView of the adapter i get my button and set setOnClickListener. From the click listener i can get the index of the clicked item.
Now my problem is that i want to propagate that information to my main activity, where i want to handle my button click.
I can save the index information in a static var, but i still don't know how to fire an event in my activity.
How do i do that?
I'm 6 days new to Android so, thanks
iggy
Code:
My Activity:
public class MyClass extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
...
myListView = (ListView)findViewById(R.id.lvxml);
myList = new MyCustomArrayAdapter(this, myAnotherClassObject);
myListView .setAdapter(myList);
...
}
}
Now in my Adapter
public class MyCustomArrayAdapter extends ArrayAdapter<myAnotherClass> {
...
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
doStuff;
}
});
}
}
I need to somehow fire a buttonclick from my main activity, but without loosing the possibility to read the index clicked in the list view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个替代方案,我认为是更优雅的解决方案。
首先,在您的
MyCustomArrayAdapter
类中,定义一个接口:在您的
ArrayAdapter
中创建一个MyCustomRowButtonListener
的成员变量,并在
ArrayAdapter
中添加一个监听器的参数 构造函数getView 方法中的
:以及您的活动中:
现在让您的活动实现 MyCustomRowButtonListener
Here's an alternative and, what I think to be, more elegant solution.
Firstly, in your
MyCustomArrayAdapter
class, define an interface:Create an member variable of
MyCustomRowButtonListener
in yourArrayAdapter
and add a parameter for the listener in the constructor
in the getView method:
and in your activity:
Now let your activity implement MyCustomRowButtonListener
所以我以某种方式解决了它。这可能不是这样做的方法,但它确实有效。
我将活动保存在静态变量中。
So I've solved it somehow. It's probably not the way to do it but it works.
I save my activity in a static var.
索引是
position
变量。Ist 必须是最终的才能在这里使用它
您可以使用侦听器接口通知您的
Activity
。The index ist the
position
variable.Ist has to be final to use it here
You can inform your
Activity
with a listener-interface.我假设你的代码看起来有点像这样。
要处理按钮单击事件,您需要有一个如下所示的 onClick 方法。
每当您单击按钮时,它都会调用 onClick 方法。这就是你可以操作静态变量、触发其他方法等的地方。我希望这会有所帮助。
I'm going to assume your code looks a little like this.
To handle button click events, you need to have an onClick method that looks like this.
Whenever you click your button, it'll invoke the onClick method. That is where you would manipulate your static var, fire off other methods, etc. I hope that helps.