Last active
March 15, 2016 18:47
-
-
Save pipiscrew/10004381 to your computer and use it in GitHub Desktop.
[android] Raise event from plain class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//source - by Rajeev - http://stackoverflow.com/a/20132526/1320686 | |
//create an interface BtnClickListener | |
public interface BtnClickListener { | |
public abstract void onBtnClick(int position, View v); | |
} | |
//on adapter pass the activity/fragment listener | |
private BtnClickListener mClickListener = null; | |
public ProfilAdapter(Context context, List<Customer> list, BtnClickListener listener) { | |
mContext = context; | |
mLayoutInflater = (LayoutInflater) mContext | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
list=lista; | |
mClickListener = listener; | |
} | |
//Now you can simply set the normal onClickListener in getView() | |
holder.product_overflow = (ImageView) convertView.findViewById(R.id.frag_offer_product_overflow); | |
holder.product_overflow.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(mClickListener != null) | |
mClickListener.onBtnClick(item.getId(), v); | |
} | |
}); | |
//on activity, when attaching the adapter to listview | |
lstv_adapter = new ProfilAdapter(getActivity(), cust_list ,new Frag_Offer_Lstv_Settings() { | |
@Override | |
public void responseResult(int position, View v) { | |
PopupMenu popupMenu = new PopupMenu(getActivity(),v); | |
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() { | |
@Override | |
public boolean onMenuItemClick(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.offer_menu_print: | |
return true; | |
case R.id.offer_menu_add_to_order: | |
return true; | |
default: | |
return true; | |
} | |
} | |
}); | |
popupMenu.inflate(R.menu.offer_menu); | |
popupMenu.show(); | |
} | |
}); | |
//source - by Rajeev - http://stackoverflow.com/a/20132526/1320686 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1-create an interface on separated class as : | |
import java.util.EventListener; | |
public interface post2socialInterface extends EventListener { | |
public void responseResult(String val); | |
} | |
//2-at original class is like that : | |
public class post2social { | |
// Used to communicate the result back to the Activity | |
public post2socialInterface listener; | |
. | |
. | |
. | |
//in a method raise the event like : | |
listener.responseResult("Message posted on " + provider); | |
. | |
. | |
. | |
//make the setListener on your own! | |
public void setListener(post2socialInterface listener) { | |
this.listener = listener; | |
} | |
} | |
//then at activity | |
post2social s = new post2social(Test.this, "", "", "", "", "", ); | |
s.setListener(new post2socialInterface() { | |
@Override | |
public void responseResult(String val) { | |
final String tmp = val; | |
runOnUiThread(new Runnable() { | |
public void run() { | |
if (progress != null) | |
progress.dismiss(); | |
Toast.makeText(TestActivity.this,tmp, Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment