I want to increase counter android in ListView adapter, but next click row always failure increased.
counting_update_cycle = actual.getJson().getCycle();
counting_update = counting_update_cycle;
holder.counting.setText(String.valueOf(counting_update_cycle));
//action tambah
holder.btntambahekekusi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.counting.setText(String.valueOf(counting_update));
counting_update++;
}
});
Please try below code:
counting_update_cycle = actual.getJson().getCycle();
counting_update = counting_update_cycle;
holder.counting.setText(String.valueOf(counting_update_cycle));
//action tambah
holder.btntambahekekusi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// added code
counting_update_cycle = actual.getJson().getCycle();
counting_update = counting_update_cycle;
holder.counting.setText(String.valueOf(counting_update));
// update instance property of object
actual.getJson().setCycle(counting_update++);
}
});
Related
I am using this code but its not working i am still able to click more than 2 times.
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
accept.setEnabled(false);
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
handler.removeCallbacks(runnable);
}
});
its not showing any error but not working as desired.
boolean run = true;
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(run) {
run = false;
//your code
}
}
});
I am new to Android. I'm trying to restrict increments if the value of stock is 10. The count value is set in textcount which is a TextView. plus and minus are button for increment and decrement.
stockshow is textview and ITEM_QTY i added in getter setter method
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count--;
textcount.setText(String.valueOf(count));
}
});
stockshow.setText(country.getITEM_QTY());
You'll want to make use of the setEnabled() method of View to ensure that the plus button can no longer be clicked should the stock count be set to 10. Likewise, you'll also want to disable the minus button once the stock count reaches 0. You could create a method like this:
private void checkBounds() {
plus.setEnabled(count < 10);
minus.setEnabled(count > 0);
}
And then use it in your listeners like so:
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
checkBounds();
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count--;
textcount.setText(String.valueOf(count));
checkBounds();
}
});
You have to add an if condition in your increment method
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count<10){
addtocart.setVisibility(View.VISIBLE);
count++;
textcount.setText(String.valueOf(count));
}
//You can add an else if you want to do something else{}
}
});
I am trying to make a app that has a switch a button and a text and if you turn the switch on and press the button; the number displayed on the text will be added by 1. But if the switch is turned off the number will be subtracted by 1.
but when i run my app and press the button, the app crashes...
i do not have much experience at programming and i do not know what im doing wrong. and i have only tried this code.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked== true){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
if (isChecked == false) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(text_int);
}
});
}
}
});
}
}
so this should behave as i described earlier but it doesn't.
Your app crashes because you are trying to set an int to a textview.setText()and when you pass an int to this method it expects it to be a resource id and which could not be found in your case that's why it will throw ResourceNotFoundException and crashes.
You should set text as following:
text.setText(String.valueOf(text_int));
You’re nesting listeners but that logic doesn’t work sequentially. You should declare your listeners separately. I suggest you create a boolean that holds the state of the switch and one button listener. Within the listener check if switch is enabled then run your calculations and do the same if the switch is disabled.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mySwitch.isChecked(){
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
} else {
String text_string = text.getText().toString();
int text_int = Integer.parseInt(text_string);
text_int++;
text.setText(String.valueOf(text_int));
}
}
});
You don't need a listener for the Switch, but only 1 listener for the Button:
final TextView text = (TextView)findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
final Switch mySwitch = (Switch)findViewById(R.id.mySwitch);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text_string = text.getText().toString();
int text_int = 0;
try {
text_int = Integer.parseInt(text_string);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (mySwitch.isChecked())
text_int++;
else
text_int--;
text.setText("" + text_int);
}
});
Every time you click the Button, in its listener the value in the TextView is increased or decreased depending on whether the Switch is checked or not.
I'm having this error out of the blue and have no idea what's causing it or where it has come from.
Basically I've got a RecyclerView that gets populated with products. When a product is selected, I've got a custom Dialog that pops up where the user can increase product quantity or remove the product. This all works, however if I click the same product a second time it crashes the app with the following error:
System services not available to Activities before onCreate()
This is my RecyclerView.Adapter with the onBindViewHolder()
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder>{
#Override
public void onBindViewHolder(#NonNull OrderAdapter.MyViewHolder holder, int position) {
final Item Item = ItemList.get(position);
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog = new Dialog(context,R.style.Custom_Theme_Dialog);
//Code breaks on this line
dialog.setContentView(R.layout.dialog_cart_edit);
cartProdDesc = dialog.findViewById(R.id.lblcartProdDesc);
cartQuantity = dialog.findViewById(R.id.edit_quantity);
btnDone = dialog.findViewById(R.id.btn_dialog_done);
btnRemove = dialog.findViewById(R.id.btn_dialog_remove);
addQuantity = dialog.findViewById(R.id.addition_action);
minusQuantity = dialog.findViewById(R.id.minus_action);
cartProdDesc.setText(cartItem.getProductDescription());
cartPackSize.setText(cartItem.getPackSize());
addQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
quantity = Integer.parseInt(cartQuantity.getText().toString());
} catch (NumberFormatException nf) {
Log.e("Number Exception","Number Is Blank");
quantity = 0;
} catch (Exception e){
Log.e("ERROR",e.toString());
}
cartQuantity.setText(String.valueOf(++quantity));
}
});
minusQuantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
quantity = Integer.parseInt(cartQuantity.getText().toString());
} catch (NumberFormatException nf) {
Log.e("Number Exception","Number Is Blank");
quantity = 0;
} catch (Exception e){
Log.e("ERROR",e.toString());
}
cartQuantity.setText(String.valueOf(--quantity));
}
});
btnDone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(cartQuantity.getText().toString().isEmpty() || cartQuantity.getText().toString().equals("0") || cartQuantity.getText().toString().contains("-")){
cartQuantity.setError("Enter a valid quantity");
} else {
newQuantity = cartQuantity.getText().toString();
db.updateCartItem(new CartItem(cartItem.getId(),cartItem.getProductCode(),cartItem.getBarcode(),cartItem.getNappiCode(),cartItem.getProductDescription(),cartItem.getPackSize(),newQuantity));
updateDataSet();
notifyDataSetChanged();
dialog.dismiss();
}
}
});
btnRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogInterface.OnClickListener dialogClickListner = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
db.deleteCartItem(cartItem.getId());
updateDataSet();
dialog.dismiss();
break;
case DialogInterface.BUTTON_NEGATIVE:
dialog.dismiss();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure you want to remove " + cartItem.getProductDescription()).setPositiveButton("Yes",dialogClickListner).setNegativeButton("No",dialogClickListner).show();
}
});
dialog.show();
}
});
}
}
I've got a private Dialog dialog; declaration further up on the Activity in case anyone was wondering.
The code breaks on the dialog = new Dialog(context,R.style.Custom_Theme_Dialog); however if I comment out the dialog.show() at the end I have no issues, apart from the dialog not showing, but that tells me that the problem isn't with the assigning of the dialog, or am I wrong on this train of thought ?
This is a line of code in my OrderActivity where I'm calling the adapter, I'm sending the context from here.
OrderAdapter = new OrderAdapter(this,ItemList);
This is my constructor where I'm assigning Context
public OrderAdapter(Context context, List<CartItem> cartItemList){
this.context = context;
this.cartItemList = cartItemList;
}
Depending on where that Context is coming from exactly, it might have already been "destroyed" by the time onClick() is called (well not really, because the Dialog is holding an implicit reference to it). In this case this is also a memory leak.
I'd suggest you to change the following:
#Override
public void onClick(View view) {
dialog = new Dialog(context, R.style.Custom_Theme_Dialog);
To this:
#Override
public void onClick(View view) {
dialog = new Dialog(view.getContext(), R.style.Custom_Theme_Dialog);
This way you'll always reference the Context the corresponding View is associated with.
I was trying to make a simple app in which clicking tapb Button increments the variable value of notaps and the reset Button sets it to 0. When I click on tapb it increments the value & clicking reset resets it but when I again click tabp it increments from the previous value.
Eg :
init value of notaps = 0;
I click tabp 3 times and notaps value = 3
I click reset and notaps value = 0
I click tabp 3 times and notaps value = 4
Button tapb = (Button)findViewById(R.id.tapb);
Button reset = (Button)findViewById(R.id.reset);
tapb.setOnClickListener(
new Button.OnClickListener(){
int notaps = 0;
#Override
public void onClick(View v) {
TextView taps = (TextView)findViewById(R.id.taps);
notaps++;
taps.setText(String.valueOf(notaps));
}
}
);
reset.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View v) {
TextView taps = (TextView) findViewById(R.id.taps);
int notaps=0;
taps.setText(String.valueOf(notaps));
}
}
);
First, you have 2 instances of int named notaps that have nothing to do with each other. Your reset button does not set the right notaps variable.
Here's a snippet that should work.
private int notaps;
Button tapb = (Button)findViewById(R.id.tapb);
Button reset = (Button)findViewById(R.id.reset);
TextView taps = (TextView)findViewById(R.id.taps);
tapb.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick(View v) {
notaps++;
taps.setText(String.valueOf(notaps));
}
}
);
reset.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View v) {
notaps = 0;
taps.setText(String.valueOf(notaps));
}
}
);
Try declaring your notaps variable globally meaning where every you declare you buttons declare your notaps variable there, here is what I mean.
Button tapb = (Button)findViewById(R.id.tapb);
Button reset = (Button)findViewById(R.id.reset);
int notaps = 0; // declare variable globally
tapb.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick(View v) {
TextView taps = (TextView)findViewById(R.id.taps);
taps.setText(String.valueOf(++notaps));
}
}
);
reset.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View v) {
TextView taps = (TextView) findViewById(R.id.taps);
taps.setText(String.valueOf(notaps=0));
}
}
);