I have onClickListener in my Custom Adapter for ListView
holder.name.setText(dataModelList.get(position).getRadio_name());
final ViewHolder finalHolder = holder;
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(adapterhandler !=null) {
adapterhandler.updateText(dataModelList.get(position).getRadio_name());
}
if(!mRadioManager.isPlaying()) {
mRadioManager.connect();
mRadioManager.startRadio(dataModelList.get(position).getRadio_url());
}
else
{
mRadioManager.stopRadio();
}
}
});
Then I made Abstract Class to update TextView
public abstract class InterfaceUpdate
{
public void updateText(String text) {}
}
and Listener in Fragment to update TextView:
#Override
public void onRadioStarted() {
adapter.adapterhandler = new InterfaceUpdate() {
#Override
public void updateText(String text) {
super.updateText(text);
title.setText(text);
}
};
What is my problem? I cannot make
adapterhandler.updateText(dataModelList.get(position).getRadio_name());
without checking if it's null, because it's generate NullPointerException
when I click on list element first time, TextView is not changing, second time is changing perfect ...;/ Any idea how to not make this adapterhandler null?
The problem is your adapterhandler is not being created until after the first time you try and call updateText. (It's not called until the onRadioStarted() callback is called by mRadioManager.startRadio)
Try this (although constructing your handler in onClick is not a great design)
holder.name.setText(dataModelList.get(position).getRadio_name());
final ViewHolder finalHolder = holder;
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (null == adapterhandler) {
adapterhandler = new InterfaceUpdate() {
#Override
public void updateText(String text) {
title.setText(text);
}
}
}
adapterhandler.updateText(dataModelList.get(position).getRadio_name());
if(!mRadioManager.isPlaying()) {
mRadioManager.connect();
} else {
mRadioManager.stopRadio();
}
}
});
And change your onRadioStarted:
#Override
public void onRadioStarted() {
mRadioManager.startRadio(dataModelList.get(position).getRadio_url());
};
Related
I am trying to add Facebook Ads(sdk 6.3.0) in my Android App
The documentation on which I base myself
https://developers.facebook.com/docs/audience-network/reference/android/com/facebook/ads/interstitialad.html/?version=v6.3.0
https://developers.facebook.com/docs/audience-network/setting-up/ad-setup/android/interstitial/
I want when the person clicks on a button it shows an ad and then a new activity when the ad end
I also use a return which directly shows the activity if no ads are available
Here are the errors I am getting
The onInterstitialDisplayed method must not be replaced by the superclass
The onInterstitialDismissed method must not be replaced by the superclass
The onError method must not be replaced by the superclass
The onAdLoaded method must not be replaced by the superclass
The onAdClicked method must not be replaced by the superclass
The onAdLoaded method must not be replaced by the superclass
What I have done
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudienceNetworkAds.initialize(this);
loadAd();
clickListener();
}
private void clickListener() {
dailyCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showInterstitialAd(1);
}
});
rewardCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showInterstitialAd(2);
}
});
}
private void loadAd(){
mInterstitialAd = new InterstitialAd(this, "750944672287722_755093405206182");
}
private void showInterstitialAd(final int i) {
if (mInterstitialAd.isAdLoaded()) {
mInterstitialAd.show();
mInterstitialAd.loadAd(new InterstitialAd.InterstitialLoadAdConfig() {
#Override
public void onInterstitialDisplayed(Ad ad) {
}
#Override
public void onInterstitialDismissed(Ad ad) {
if (i == 1)
{
dailyCheck();
}
if (i == 2)
{
startActivity(new Intent(MainActivity.this, RewardActivity.class));
}
}
#Override
public void onError(Ad ad, AdError adError) {
}
#Override
public void onAdLoaded(Ad ad) {
}
#Override
public void onAdClicked(Ad ad) {
}
#Override
public void onLoggingImpression(Ad ad) {
}
});
return;
}
if (i == 1)
{
dailyCheck();
}
if (i == 2)
{
startActivity(new Intent(MainActivity.this, RewardActivity.class));
}
}
Any help is good to take!
Thank you
I want to add a interface in my fragment and my bottom sheet and when bottom sheet done an action(like select a button) it call in my background fragment but when i call interface it returns null and never call when my action done!
this is my code to fill interface , but the condition never called:
if(responseListener != null){
responseListener.onData( 200,message);
}
my code:
public class FilterBottomSheet extends BottomSheetDialogFragment implements View.OnClickListener {
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
#Override public void onStateChanged(#NonNull View bottomSheet,int newState){
if(newState==BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
}
#Override public void onSlide(#NonNull View bottomSheet,float slideOffset){}};
#SuppressLint("RestrictedApi")
#Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_filter, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent())
.getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnDoAction:
fillActionInterface("TEST");
dismiss();
break;
}
}
public OnResponseListener responseListener;
private void fillActionInterface(String message) {
if (responseListener != null) {
responseListener.onData(200, message);
}
}
}
So I can't call interface in my another fragment. (because its never calls)
You should initialize your interface instance in onAttach method of a BottomSheet:
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
responseListener = (ResponseListener) getParentFragment();
} catch(Exception e) {
//handle exception
}
}
Please be aware that if you want to show BottomSheet dialog from fragment and get a callback in the fragment you need to do it with a getChildFragmentManager() instead of getFragmentManager() call.
If you show it with getFragmentManager() you will get cast exception in onAttach method.
Take a look at this link to see difference between ChildFragmentManager and FragmentManager.
I'm trying, but when I implement both at the same time, the ViewHolder cannot reach none of those. Is it possible? My code:
public final class SomeViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener, View.OnLongClickListener {
public final AestheticTextView mName;
public final AestheticTextView mNumber;
public SomeViewHolder(View itemView) {
super(itemView);
mName = (AestheticTextView) itemView.findViewById(android.R.id.title);
mNumber = (AestheticTextView) itemView.findViewById(android.R.id.summary);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
#Override
public void onClick(View view) {
// doesn't run
}
#Override
public boolean onLongClick(View view) {
// doesn't run
}
}
If I remove one of the listeners, like below, it works fine.
public final class SomeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final AestheticTextView mName;
public final AestheticTextView mNumber;
public SomeViewHolder(View itemView) {
super(itemView);
mName = (AestheticTextView) itemView.findViewById(android.R.id.title);
mNumber = (AestheticTextView) itemView.findViewById(android.R.id.summary);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
// run properly
}
}
Create a bind method or whatever you want to call it.
public void bind(ModelClass model, MyInterface listener) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Create your own interface and add arguments based on your needs
listener.itemClicked(view);
}
});
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Create your own interface and add arguments based on your needs
listener.itemLongClicked(view);
}
});
}
On your onBindViewHolder()
holder.bind(item.get(position), listener);
And when declaring your Adapter
adapter = new MyAdapter(item, new OnItemClick() {
#Override
public void itemClicked(View view, ModelClass model) {
//event
}
#Override
public void itemLongClicked(View view, ModelClass model) {
return true;
}
});
I have Android module library which will create the Touch Keypad UI and set event listener for done and backbutton.
Also have main activity with implement eventCallback interface
MainActivity.java (Appication)
public class MainActivity extends AppCompatActivity implements eventCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = new touchkey(this);
setContentView(v);
}
#Override
public void onClick() {
Log.i("test","complete");
Toast.makeText(this, "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
}
}
eventCallback.java (Android Module Library)
public interface eventCallback {
void onClick();
}
touchkey.java (Android Module Library)
public class touchkey extends RelativeLayout{
private static touchkey INSTANCE;
TextView bclear;
ImageView bdone;
eventCallback eventCall;
public touchkey(Context context) {
super(context);
initialize(context);
}
public touchkey(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public void test() {
Log.i("test","test");
}
private void initialize(Context context) {
inflate(context, R.layout.touchkey, this);
bclear = (TextView) findViewById(R.id.anti_theft_t9_key_clear);
bdone = (ImageView) findViewById(R.id.anti_theft_t9_key_done);
bdone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (eventCall != null) {
eventCall.onClick();
}
}
});
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
but iam getting nullpointer exception on the touchkey.java
eventCall.onClick(); (eventCall is null)
I dont know where i am doing things wrong. can anybody help on this. Requirement: i need to handle the click event happening on the Library in Main Activity
You must create setter for eventCall (in touchkey.java):
public void setEventCall(eventCallback eventCall) {
this.eventCall = eventCall;
}
And, use it (in MainActivity):
View v = new touchkey(this);
setContentView(v);
((touchkey) v).setEventCall(this);
Still i am calling startUpload(position); method in getView(...) {} method, but now i wanna call it in
loginbutton.setOnClickListener(new View.OnClickListener() {....}
Like this, but always i am getting position cannot be resolved to a variable
so here i want to know how i should need to write method to use it anywhere in a class:
loginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SaveData();
alertDialog.dismiss();
startUpload(position);
}
below is the method, startUpload(...){...}
//Upload
public void startUpload(final int position) {
Runnable runnable = new Runnable() {
public void run() {
handler.post(new Runnable() {
public void run() {
View v = lstView.getChildAt(position - lstView.getFirstVisiblePosition());
// Show ProgressBar
ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar);
progress.setVisibility(View.VISIBLE);
// Status
TextView status = (TextView)v.findViewById(R.id.ColStatus);
status.setText("Uploading..");
new UploadFileAsync().execute(String.valueOf(position));
}
});
}
};
new Thread(runnable).start();
}
Try using the following sintax.
loginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SaveData();
alertDialog.dismiss();
startUpload(OldUploadActivity.this.position);
}
}
You need to add final int position as a parameter to onPrepareDialog() as well; just like you're passing it to the getView() method. Also, update the line of code from where you're actually invoking this method from by passing the actual position value.
public void onPrepareDialog(final int position, int id, Dialog dialog) {