Buttons not working at first and last index? - java

I am working on an application in which have two navigation button namely 'previous and next' which on tap load stories respectively. When i tap these buttons gently they work fine button when i tap next button to the last index continuously without any break then the previous button does not work or similarly when i start tapping previous button to the very first index i am unable to move forward.
Remember this only happens at the extreme cases, onClick is not called, don't know why.
My code is as follows, please help me out. Thanks in advance.
this the code of onClick, which works fine in all cases except the extreme cases, when buttons are not tapped gently.
public void onClick(View v) {
if (visible == true) {
Log.e("visible", "true");
return;
}
visible = true;
try {
Log.e("Now", "On Click");
pDialog = new ProgressDialog(StoriesListController.this);
pDialog.setIndeterminate(true);
pDialog.setIcon(R.drawable.icon_small);
pDialog.setCancelable(true);
if (isFavoriteList == true) {
pDialog.setMessage("Loading favorites...");
} else {
pDialog.setMessage("Loading data...");
}
pDialog.setTitle("Please wait");
pDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
if (cancelableHeavyWorker != null) {
cancelableHeavyWorker.setHandler(null);
}
finish();
}
});
pDialog.show();
if (v == next) {
if (ind < items.size() - 1) {
ind++;
loadAndShowNextActivity();
}
} else if (v == previous) {
if (ind > 0) {
ind--;
loadAndShowNextActivity();
}
}
// realeaseMemoryIfneededofStory(ind);
} catch (Exception e) {
Log.e("Error", "--.OnClink Message" + e.toString());
e.printStackTrace();
}
}
Moreover, the boolean variable "visible" is being set to false in the callback function.
The Code of Call Back Function is as follows:
private void fetchTopStoryDetailCallback(Object resultVector) {
System.out.println("fetchTopStoryDetailCallback");
try {
Vector<?> v = (Vector<?>) resultVector;
boolean completedOrFailed = ((Boolean) v.elementAt(0)).booleanValue();
if (completedOrFailed == true) {
boolean slideshow = ((Boolean) v.elementAt(1)).booleanValue();
Object result = v.elementAt(2);
String res[] = (String[]) result;
if (slideshow) {
if (Utils.topStorySlidesArrayList != null && Utils.topStorySlidesArrayList.size() > 0) {
Intent articleActivityIntent = new Intent(this, SlideShowActivity.class);
articleActivityIntent.putExtra("storyData", (String[]) result);
articleActivityIntent.putExtra("back", back);
articleActivityIntent.putStringArrayListExtra("sid", slideids);
// articleActivityIntent.putExtra("contentType", )
articleActivityIntent.putExtra("addkey", ADDS_KEY);
showActivityInContoller(articleActivityIntent);
hidePrgressgingDailog();
} else {
hidePrgressgingDailog();
this.closeActivity = true;
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
}
} else {
if (res[3] != null && (!(res[3].equalsIgnoreCase("null")) && (!res[3].equals("")))) {
Intent slideshowActivtyIntent = new Intent(this, ArticleActivity.class);
slideshowActivtyIntent.putExtra("storyData", (String[]) result);
slideshowActivtyIntent.putExtra("back", back);
slideshowActivtyIntent.putExtra("addkey", ADDS_KEY);
showActivityInContoller(slideshowActivtyIntent);
hidePrgressgingDailog();
} else {
hidePrgressgingDailog();
this.closeActivity = true;
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
}
}
} else {
showMessage("Error", "This story encounter an error while opening. Check your Internet Connection and try later");
hidePrgressgingDailog();
}
} catch (Exception e) {
Log.e("StoriesController", "Message = " + e.toString());
e.printStackTrace();
}
finally {visible = false; }
}
this is how visiblity of the buttons is set...!!!!
private void adjustButtonsVisibility() {
try {
if (items.size() == 1) {
next.setEnabled(false);
next.setImageResource(R.drawable.navigator_next_disable);
previous.setEnabled(false);
previous.setImageResource(R.drawable.navigator_previous_disable);
//hidePrgressgingDailog();
return;
}
if (ind == 0) {
previous.setEnabled(false);
next.setEnabled(true);
previous.setImageResource(R.drawable.navigator_previous_disable);
next.setImageResource(R.drawable.story_next_arrow);
hidePrgressgingDailog();
} else if (ind > 0 && ind < items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(true);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.story_next_arrow);
}
if (ind == items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(false);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.navigator_next_disable);
hidePrgressgingDailog();
}
} catch (Exception e) {
Log.e("Error", "--,adjustButtonsVisibility Message = " + e);
e.printStackTrace();
}
}

Do this
if (ind == 0) {
previous.setEnabled(false);
previous.setImageResource(R.drawable.navigator_previous_disable);
next.setImageResource(R.drawable.story_next_arrow);
hidePrgressgingDailog();
}
else if (ind > 0 && ind < items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(true);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.story_next_arrow);
}
else if (ind == items.size() - 1) {
previous.setEnabled(true);
next.setEnabled(false);
previous.setImageResource(R.drawable.story_previous_arrow);
next.setImageResource(R.drawable.navigator_next_disable);
hidePrgressgingDailog();
}

Related

I want to automatically reload function after setOnClickListener.. here is my code below

Here is my code below:
tvWatch.setOnClickListener(v -> {
earnCreditDialog.dismiss();
RewardedAd rewardedAd = adMobUtils().getRewardedAd();
if (rewardedAd != null /*&& rewardedAd.isLoaded()*/) {
isEarnCredit = true;
rewardedAd.show(TournamentActivity.this, rewardedAdCallback);
rewardedAd.setFullScreenContentCallback(setFullScreenContentCallback);
} else {
CommonUtils.showMessage(TournamentActivity.this, getString(R.string.earn_credit_not_available));
if (rewardedAd == null) {
Timber.e("Create New Rewarded Ad");
loadRewardVideoAd(true);
} else {
Timber.e("Loading Current Rewarded Ad");
}
}
});
I would also like to add a dialog that I have coded already before the rewarded ad is shown. Here is the code below:
public void showAdsLoadingProgressDialog() {
if (adsLoadingDialog == null || !adsLoadingDialog.isShowing()) {
adsLoadingDialog = new Dialog(BaseActivity.this);
Window window = adsLoadingDialog.getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
adsLoadingDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
adsLoadingDialog.setContentView(R.layout.custom_ads_loading_dialog_layout);
adsLoadingDialog.show();
adsLoadingDialog.setCancelable(false);
adsLoadingDialog.setCanceledOnTouchOutside(false);
}
}

How to switch the data onClick of button?

I have a list of items of object. I am creating dynamic buttons as per the size of a list using for loop.
I am showing some data when its the first item of the loop. Now /i want to switch the data when I will click the buttons.
Buttons will have the numbers of the item, if three items are present in the list , three buttons are there named one, two, three.
So if I click on first I want to show data of first item, if second clicked data of second item respectively.
I tried to do it, but I am not able to get the result.
#Override
public void doPostExecute(List<Payment> list1)
{
list.addAll(list1);
for(final Payment payment : list) {
i++;
Button myButton = new Button(getActivity());
LinearLayoutCompat.LayoutParams lp = new LinearLayoutCompat.LayoutParams(60,30);
linear_buttons.addView(myButton, lp);
myButton.setText(String.valueOf(i));
myButton.setBackgroundResource(R.drawable.edit_button);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (payment.getPaidAmount() != null && !payment.getPaidAmount().equals("null"))
txt_paid.setText("$" + payment.getPaidAmount());
if (payment.getProductStatus() != null && !payment.getProductStatus().equals("null")) {
if (payment.getProductStatus().equals("1")) {
txt_status.setText(getString(R.string.paid));
button_complain.setVisibility(View.VISIBLE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("0")) {
txt_status.setText(getString(R.string.unPaid));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("5")) {
txt_status.setText(getString(R.string.processing));
button_complain.setVisibility(View.VISIBLE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("6")) {
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
txt_status.setText(getString(R.string.releasedStatus));
} else if (payment.getProductStatus().equals("8")) {
txt_status.setText(getString(R.string.disputeStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
} else if (payment.getProductStatus().equals("9")) {
txt_status.setText(getString(R.string.disputeResolvedStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
} else if (payment.getProductStatus().equals("2")) {
txt_status.setText(getString(R.string.completedStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
}
if (payment.getTpAmount() != null && !payment.getTpAmount().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTpAmount()));
txt_tp_amount.setText("$" + amount);
}
if (payment.getCommission() != null && !payment.getCommission().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getCommission()));
txt_comission.setText("$" + amount);
}
if (payment.getTax() != null && !payment.getTax().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTax()));
txt_tax.setText("$" + amount);
}
if (payment.getPayeEmail() != null && !payment.getPayeEmail().equals("null")) {
txt_payee_email.setText(payment.getPayeEmail());
}
if (payment.getPayerEmail() != null && !payment.getPayerEmail().equals("null")) {
txt_payer_email.setText(payment.getPayerEmail());
}
if (payment.getPayerName() != null && !payment.getPayerName().equals("null")) {
txt_payer_name.setText(payment.getPayerName());
}
if (payment.getVendorId() != null && !payment.getVendorId().equals("null")) {
txt_vendor_id.setText(payment.getVendorId());
}
}
}
});
if(i==1) {
if (payment.getPaidAmount() != null && !payment.getPaidAmount().equals("null"))
txt_paid.setText("$" + payment.getPaidAmount());
if (payment.getProductStatus() != null && !payment.getProductStatus().equals("null")) {
if (payment.getProductStatus().equals("1")) {
txt_status.setText(getString(R.string.paid));
button_complain.setVisibility(View.VISIBLE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("0")) {
txt_status.setText(getString(R.string.unPaid));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("5")) {
txt_status.setText(getString(R.string.processing));
button_complain.setVisibility(View.VISIBLE);
button_more.setVisibility(View.GONE);
} else if (payment.getProductStatus().equals("6")) {
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
txt_status.setText(getString(R.string.releasedStatus));
} else if (payment.getProductStatus().equals("8")) {
txt_status.setText(getString(R.string.disputeStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
} else if (payment.getProductStatus().equals("9")) {
txt_status.setText(getString(R.string.disputeResolvedStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
} else if (payment.getProductStatus().equals("2")) {
txt_status.setText(getString(R.string.completedStatus));
button_complain.setVisibility(View.GONE);
button_more.setVisibility(View.VISIBLE);
}
if (payment.getTpAmount() != null && !payment.getTpAmount().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTpAmount()));
txt_tp_amount.setText("$" + amount);
}
if (payment.getCommission() != null && !payment.getCommission().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getCommission()));
txt_comission.setText("$" + amount);
}
if (payment.getTax() != null && !payment.getTax().equals("null")) {
String amount = String.format(Locale.US, "%.2f", Double.parseDouble(payment.getTax()));
txt_tax.setText("$" + amount);
}
if (payment.getPayeEmail() != null && !payment.getPayeEmail().equals("null")) {
txt_payee_email.setText(payment.getPayeEmail());
}
if (payment.getPayerEmail() != null && !payment.getPayerEmail().equals("null")) {
txt_payer_email.setText(payment.getPayerEmail());
}
if (payment.getPayerName() != null && !payment.getPayerName().equals("null")) {
txt_payer_name.setText(payment.getPayerName());
}
if (payment.getVendorId() != null && !payment.getVendorId().equals("null")) {
txt_vendor_id.setText(payment.getVendorId());
}
}
}
}
}
How can I achieve this? please help.. Thank you.
You can set an onClickLister to the button in that row which will have a reference to that row's data. Use an intent to start the detailed activity and add the clicked item's data as an extra.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Inflate your row
button.setOnClickListener(new OnClickListener(){
#Override
//On click function
public void onClick(View view) {
HashMap<String, String> item = contactsList.get(position);
//Create the intent to start another activity
Intent intent = new Intent(view.getContext(), DetailActivity.class);
intent.putExtra("data", item);
view.getContext().startActivity(intent);
}
});
});
in the receiving activity, you can get the data from the intent
HashMap<String, String> data = getIntent().getExtras().getParcelableExtra("data");
You are using the variable i as a global variable and using i++ inside the loop. So inside the onClick method the if(i == 1) is either always false, or always true.
Make variable local and use myButton.setId(i); inside your for loop where you create buttons.
Now inside OnClickListener you can use int id = v.getId(); and use if(id == 1)

Updating handleEvent(e) to AWTEvent

The first snippet is my first try before I learned it was deprecated and attempted to switch to AWT in the second snippet
public boolean handleEvent (Event e) {
if ((e.target == input) && (e.id == Event.ACTION_EVENT)) {
try {
o.writeUTF((String) e.arg);
o.flush();
} catch (IOException ex) {
ex.printStackTrace();
listener.interrupt();
}
input.setText("");
return true;
} else if((e.target == this) && (e.id == Event.WINDOW_DESTROY)) {
if (listener != null)
listener.interrupt();
setVisible(false);
return true;
}
return super.handleEvent(e);
}
The next part I tried below changed it to AWT which is what I'm aiming for but this is when the "cannot be resolved or is not a field" errors came up.I tried processEvent(); but that gave me "cannot be resolved or is not a field" errors on each instance of "target" and "arg", also a "Cannot return a void result" on return super.processEvent(e);. Does anyone know a way I can update this?
public boolean handleEvent (AWTEvent e) {
if ((e.target == input) && (AWTEvent.RESERVED_ID_MAX == Event.ACTION_EVENT)) {
try {
o.writeUTF((String) e.arg);
o.flush();
} catch (IOException ex) {
ex.printStackTrace();
listener.interrupt();
}
input.setText("");
return true;
} else if((e.target == this) && (AWTEvent.RESERVED_ID_MAX == Event.WINDOW_DESTROY)) {
if (listener != null)
listener.interrupt();
setVisible(false);
return true;
}
return super.processEvent(e);
}

How to loop through imageButtons correctly?

Hey guys so I have 3 imageButton icons/tags I want to set. So I do a loop and go through them. Now the user can press a number of tags (such as a food tag, retail tag, housing tag, etc) and that adds to a Global ArrayList. Now if the user only pressed 1 tag and there's three imageButtons that need to be set, I want to set the first imagebutton to the only tag they picked and set the rest to a white blank image, but I keep getting the following error:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
here is my loop I dunno what I'm doing wrong here.
private void getIcons()
{
iconArray.add(icon1);
iconArray.add(icon2);
iconArray.add(icon3);
for(int i = 0; i < iconArray.size(); i++)
{
ImageView button= iconArray.get(i);
if(Global_Class.getInstance().getValue().tags.size() == 1)
{
if(Global_Class.getInstance().getValue().tags.get(i) == null)//Here is where Its giving me an ERROR!!!!!
{
button.setImageResource(R.drawable.icon_blank);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
}
else if(Global_Class.getInstance().getValue().tags.size() == 2)
{
if(Global_Class.getInstance().getValue().tags.get(i) == null)
{
button.setImageResource(R.drawable.icon_blank);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
else
{
//
}
}
else
{
if(Global_Class.getInstance().getValue().tags.get(i) == "food")
{
button.setImageResource(R.drawable.white_small_icon_food);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "bar")
{
button.setImageResource(R.drawable.white_small_icon_bar);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "club")
{
button.setImageResource(R.drawable.white_small_icon_club1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "housing")
{
button.setImageResource(R.drawable.white_small_icon_housing);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "sports")
{
button.setImageResource(R.drawable.white_small_icon_sports);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "retail")
{
button.setImageResource(R.drawable.white_small_icon_retail);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "outdoors")
{
button.setImageResource(R.drawable.white_small_icon_outdoors1);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "education")
{
button.setImageResource(R.drawable.white_icon_education);
}
else if(Global_Class.getInstance().getValue().tags.get(i) == "landmark")
{
button.setImageResource(R.drawable.white_small_icon_landmark);
}
else
{
//
}
}
}
}
Here is where i make tags arraylist:
public class GlobalVariables
{
public ArrayList<String> tags = new ArrayList<>();
}
And I add to it in another class:
public void onClick(View v)
{
switch (v.getId())
{
case R.id.imageButton:
if(food_pressed)
{
ImageButton food_button = (ImageButton) findViewById(R.id.imageButton);
food_button.setImageResource(R.drawable.pressed_food);
tags.add("food");
food_pressed = false;
break;
}
else
{
ImageButton food_button = (ImageButton) findViewById(R.id.imageButton);
food_button.setImageResource(R.drawable.icon_food);
tags.remove("food");
food_pressed = true;
break;
}
case R.id.imageButton9:
ImageButton done_button = (ImageButton) findViewById(R.id.imageButton9);
done_button.setImageResource(R.drawable.pressed_done);
Global_Class.getInstance().getValue().tags = tags;
//Toast.makeText(getApplicationContext(),Global_Class.getInstance().getValue().tags.toString(),Toast.LENGTH_SHORT).show();
startActivity(toDescription);
break;
etc...
}

Java variable initialization in if-then statement

I have this code that is suppose to turn off Wifi via a toggle button. I also want to have it so that if the user already had Wifi on before pressing the button, the Wifi would go back on after turning off the toggle button. This is done through the wifiON boolean. However, since the variable is initialized in the first part of the if statement, it won't be used by the else statement. How can I set it so that the else statement can get the value of the boolean from the if statement. Below is the code.
public void airplaneClicked (View view) {
boolean on = ((ToggleButton) view).isChecked();
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
boolean wifiOn;
if (on) {
if (wifi.isWifiEnabled()) {
wifiOn = true;
Log.v("", "" + wifiOn);
wifi.setWifiEnabled(false);
} else {
wifiOn = false;
Log.v("", "" + wifiOn);
}
}
else {
if (wifiOn == true) {
wifi.setWifiEnabled(true);
}
}
}
wifi.setWifiEnabled(!wifi.isWifiEnabled());
If you need the value of wifiOn in both parts of the if-else, move it to be before the if-else.
if (wifi.isWifiEnabled()) {
wifiOn = true;
} else {
wifiOn = false;
}
if (on) {
if (wifi.isWifiEnabled()) {
Log.v("", "" + wifiOn);
wifi.setWifiEnabled(false);
} else {
Log.v("", "" + wifiOn);
}
}
else {
if (wifiOn == true) {
wifi.setWifiEnabled(true);
}
}
//Use additional boolean - wifiState
boolean wifiState = wifi.isWifiEnabled();
if (on) {
if (wifiState) {
wifiState = true;
Log.v("", "" + wifiOn);
wifi.setWifiEnabled(false);
} else {
wifiState = false;
Log.v("", "" + wifiOn);
}
}
else {
if (wifiState) {
wifi.setWifiEnabled(true);
}
}

Categories

Resources