如何从 listView 中按下的按钮访问 main Activity 中的数据?

How to access data in main Activity from a button pressed in listView?

所以我有一个带有自定义适配器的 listView,我希望每当我按下一个按钮时,我都可以从 MainActivity 的 OnItemClickListener 中收听它。

所以我的问题是,在我的 ListView 中的 Button 上调用 onClick 时我会传递什么,以便我可以从主 Activity 读取它?

 @Override
public View getView(final int pos, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    final CustomProductAdapter p=this;
    final View rowView;
    rowView = inflater.inflate(R.layout.product_list_test, null);
    holder.productView=(TextView) rowView.findViewById(R.id.productView);
    holder.descView=(TextView) rowView.findViewById(R.id.descView);
    holder.imageView=(ImageView) rowView.findViewById(R.id.imageView);
    holder.productCost=(TextView) rowView.findViewById(R.id.productCost);
    holder.iButton=(ImageButton) rowView.findViewById(R.id.imageButton);
    holder.iButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


           //What will I put here?


        }
    });
    holder.productView.setText(proNames[pos]);
    holder.descView.setText(R.string.description_product);
    holder.productCost.setText("0.00");
    ImageView image =holder.imageView;
    imageloader.DisplayImage(images[pos],image);

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "You Clicked "+proNames[pos], Toast.LENGTH_SHORT).show();
        }
    });
    return rowView;
}

以便我可以像这样从这里阅读它(我不知道这是不是正确的方式)

 listView = (ListView)findViewById(R.id.listView);
    listView.setAdapter(new CustomProductAdapter(this, images, proNames1, desc));
     AdapterView.OnItemClickListener mMessageClickedHandler = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            // Do something in response to the click
            long ids=v.getId();
            if(ids==R.id.imageButton){
                Toast.makeText(getBaseContext(),"Button clicked at "+position,Toast.LENGTH_SHORT).show();
            }
        }
    };
    listView.setOnItemClickListener(mMessageClickedHandler);

在我的 MainActivity.java 中。我是初学者,如果我做的不正确,请帮助我。

使用接口进行通信是实现此目的的方法之一。

//create an interface
interface ButtonClickNotify{
   void onBUttonClick(int position);
}

然后在main里实现activity

public calss MainActivity extends AppCompatActivity implements ButtonClickNotify{

    //Override the interface method
    @Override
    public void onButtonClick(int position){
        //do something
    }
}

然后从适配器调用接口。

//declare an instance variable
ButtonClickNotify buttonNotify;
//Initiate the iterface in the constructor
public MyListAdapter(Context context /*and other parameters*/){
   //after other constructor methods including calling super
   try{
        buttonNotify=(MainActivity)context;
   }catch(Throwable e){
       //interface is not implemented
   }
}

//at the button onclick
holder.iButton.setOnClickListener(new View.OnClickListener{
     @Overrride 
     public void onClick(View v){
         try{
            buttonNotify.onButtonClick(position);
         }catch (Throwable e){
            //interface can be null
         }
     }
});

而且您不需要在 rowView 上设置 onClick,因为 listView.setOnItemClickListener 也是如此。