简体   繁体   中英

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

So I have a listView with a custom adapter and I want that whenever I press a button I could listen it from the OnItemClickListener from the MainActivity.

So my question is what will I pass while calling onClick on the Button present in my ListView so that I can read it from the main 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;
}

so that I can read it from here like this(I dont know if it is the corect way)

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

in my MainActivity.java. I am a beginner so please help me out if I am not doing it correct.

Using an interface to communicate is one of the ways to achieve this.

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

Then implement it in the main activity

public calss MainActivity extends AppCompatActivity implements ButtonClickNotify{

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

Then call the interface from the adapter.

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

And you don't need to have an onClick on rowView as listView.setOnItemClickListener does the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM