在我的活动中单击自定义数组适配器的传播按钮

发布于 2024-11-18 20:29:47 字数 1131 浏览 4 评论 0原文

我的活动有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

第几種人 2024-11-25 20:29:47

这是一个替代方案,我认为是更优雅的解决方案。

首先,在您的MyCustomArrayAdapter类中,定义一个接口:

public interface MyCustomRowButtonListener{
  void onCustomRowButtonClick(MyAnotherClass selectedItem, int position, View view);
}

在您的ArrayAdapter中创建一个MyCustomRowButtonListener的成员变量

public class MyCustomArrayAdapter{

  private MyCustomRowButtonListener mRowButtonListener;
  //....
}

,并在ArrayAdapter中添加一个监听器的参数 构造函数

public MyCustomArrayAdapter(Context context, MyCustomRowButtonListener listener){

  mContext = context;
  mRowButtonListener = listener;

}

getView 方法中的

@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) {
             mRowButtonListener.onCustomRowButtonClick(getItem(position),position,b);
         }
 });
}

:以及您的活动中:

myList = new MyCustomArrayAdapter(this, myAnotherClassObject,this);

现在让您的活动实现 MyCustomRowButtonListener

public class MyClass extends Activity implements MyCustomRowButtonListener{
...
    public void onCreate(Bundle savedInstanceState) {
    ...
    myListView = (ListView)findViewById(R.id.lvxml);
    myList = new MyCustomArrayAdapter(this, myAnotherClassObject,this);
    myListView .setAdapter(myList);
    ...
    }
}

public void onCustomRowButtonClick(MyAnotherClass selectedItem, int position, View view){
    Toast.makeText(this,"You have selected "+selectedItem,Toast.LENGTH_SHORT).show();
}

Here's an alternative and, what I think to be, more elegant solution.

Firstly, in your MyCustomArrayAdapter class, define an interface:

public interface MyCustomRowButtonListener{
  void onCustomRowButtonClick(MyAnotherClass selectedItem, int position, View view);
}

Create an member variable of MyCustomRowButtonListener in your ArrayAdapter

public class MyCustomArrayAdapter{

  private MyCustomRowButtonListener mRowButtonListener;
  //....
}

and add a parameter for the listener in the constructor

public MyCustomArrayAdapter(Context context, MyCustomRowButtonListener listener){

  mContext = context;
  mRowButtonListener = listener;

}

in the getView method:

@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) {
             mRowButtonListener.onCustomRowButtonClick(getItem(position),position,b);
         }
 });
}

and in your activity:

myList = new MyCustomArrayAdapter(this, myAnotherClassObject,this);

Now let your activity implement MyCustomRowButtonListener

public class MyClass extends Activity implements MyCustomRowButtonListener{
...
    public void onCreate(Bundle savedInstanceState) {
    ...
    myListView = (ListView)findViewById(R.id.lvxml);
    myList = new MyCustomArrayAdapter(this, myAnotherClassObject,this);
    myListView .setAdapter(myList);
    ...
    }
}

public void onCustomRowButtonClick(MyAnotherClass selectedItem, int position, View view){
    Toast.makeText(this,"You have selected "+selectedItem,Toast.LENGTH_SHORT).show();
}
半边脸i 2024-11-25 20:29:47

所以我以某种方式解决了它。这可能不是这样做的方法,但它确实有效。

我将活动保存在静态变量中。

Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setTag(position);
b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             int i = (Integer) v.getTag();
             myStaticVarRep.myActivity.myMethod(i);
         }
 });

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.

Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setTag(position);
b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             int i = (Integer) v.getTag();
             myStaticVarRep.myActivity.myMethod(i);
         }
 });
巷子口的你 2024-11-25 20:29:47

索引是position变量。
Ist 必须是最终的才能在这里使用它

public View getView(最终int位置,View ConvertView,ViewGroup父级)

您可以使用侦听器接口通知您的Activity

The index ist the position variable.
Ist has to be final to use it here

public View getView(final int position, View convertView, ViewGroup parent)

You can inform your Activity with a listener-interface.

漫雪独思 2024-11-25 20:29:47

我假设你的代码看起来有点像这样。

public class <Your Class> ... implements OnClickListener{

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        .
        .
        .
        button.setOnClickListener(this);
    }
}

要处理按钮单击事件,您需要有一个如下所示的 onClick 方法。

public void onClick(View v){
    <Handle your button click in here>
}

每当您单击按钮时,它都会调用 onClick 方法。这就是你可以操作静态变量、触发其他方法等的地方。我希望这会有所帮助。

I'm going to assume your code looks a little like this.

public class <Your Class> ... implements OnClickListener{

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        .
        .
        .
        button.setOnClickListener(this);
    }
}

To handle button click events, you need to have an onClick method that looks like this.

public void onClick(View v){
    <Handle your button click in here>
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文