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

时间:2016-06-11 01:37:21

标签: java android listview custom-adapter

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

所以我的问题是,当我在ListView中调用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中。我是初学者,所以如果我做得不对,请帮助我。

1 个答案:

答案 0 :(得分:1)

使用界面进行沟通是实现这一目标的方法之一。

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

然后在主要活动中实施

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
         }
     }
});

并且您onClick rowView上的listView.setOnItemClickListener也不需要{{1}}。{/ p>