I am using listView to change the value
here is my code. But It's not going to stop. when Listview last index no. suppose 10 then, it's crash. how do i stop next button when listView end.
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtOne.setText(letter[lastView+1]);
txtTwo.setText(letterDetails[lastView+1]);
txtThree.setText(letterB[lastView+1]);
lastView++;
if (lastView == letter.length){
lastView=0;
Toast.makeText(getApplicationContext(), lastView+" ", Toast.LENGTH_SHORT).show();
}
}
});
btnPrevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtOne.setText(letter[lastView-1]);
txtTwo.setText(letterDetails[lastView-1]);
txtThree.setText(letterB[lastView-1]);
lastView--;
}
});
Basically letter.length return size of array. Suppose you have 10 items in array then the index will be 0-9.The reason behind your app is crashing is due to array out of bound error.
What you have to do is just add this line in your if condition.
Code
if (lastView == (letter.length-1))
Related
i've this issue made me crazy. When retrieved JSON data i will use this my adapter:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof OriginalViewHolder) {
final DomandaQuizTipo1 domandaQuiz1 = items.get(position);
OriginalViewHolder vItem = (OriginalViewHolder) holder;
/* Getting each answer from array of answer */
Risposta1 risposta1 = domandaQuiz1.getRisposte().get(0);
Risposta1 risposta2 = domandaQuiz1.getRisposte().get(1);
Risposta1 risposta3 = domandaQuiz1.getRisposte().get(2);
/* Get and set text of question */
vItem.domanda.setTypeface(typefaceRegular);
vItem.domanda.setText(domandaQuiz1.getDomanda());
/* Get and set text of answer 1 */
vItem.risposta1.setText(risposta1.getRisposta());
vItem.risposta1.setTextOn(risposta1.getRisposta());
vItem.risposta1.setTextOff(risposta1.getRisposta());
/* Get and set text of answer 2 */
vItem.risposta2.setText(risposta2.getRisposta());
vItem.risposta2.setTextOn(risposta2.getRisposta());
vItem.risposta2.setTextOff(risposta2.getRisposta());
/* Get and set text of answer 3 */
vItem.risposta3.setText(risposta3.getRisposta());
vItem.risposta3.setTextOn(risposta3.getRisposta());
vItem.risposta3.setTextOff(risposta3.getRisposta());
/* When i click the first answer uncheck 2nd and 3rd */
vItem.risposta1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(0).getPunteggio();
MainActivity.UpdatePunteggioTotale();
vItem.risposta2.setChecked(false);
vItem.risposta3.setChecked(false);
}
});
/* When i click the second answer uncheck 1st and 3rd */
vItem.risposta2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(1).getPunteggio();
MainActivity.UpdatePunteggioTotale();
Log.d("adapterposition", "POS" + position);
vItem.risposta1.setChecked(false);
vItem.risposta3.setChecked(false);
}
});
/* When i click the third answer uncheck 1st and 2nd */
vItem.risposta3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position] = items.get(position).getRisposte().get(2).getPunteggio();
MainActivity.UpdatePunteggioTotale();
vItem.risposta1.setChecked(false);
vItem.risposta2.setChecked(false);
}
});
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
My issue is this: in a recyclerview of each question that contains 3 answers to choose, when i click on one of answer (ex. first toggle button) is checked correctly but when i scroll recycler view i see other question with answer (ex. first toggle button) checked.
How can i avoid this issue? Take a look from this gif i putted below.
Check gif this please to understand
You should add a field like a selected (local not server) and check that's is selected and change item ui
if(vItem.selected==1)
vItem.risposta1.setChecked(true);
else
vItem.risposta1.setChecked(false);
if(vItem.selected==2)
vItem.risposta2.setChecked(true);
else
vItem.risposta2.setChecked(false);
if(vItem.selected==3)
vItem.risposta3.setChecked(true);
else
vItem.risposta3.setChecked(false);
vItem.risposta1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=1;
notifyItemChange(adapterPosition);
}
});
vItem.risposta2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=2;
notifyItemChange(adapterPosition);
}
});
vItem.risposta3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.punteggiDomandeQuiz1[position].selected=3;
notifyItemChange(adapterPosition);
}
});
You have to set the status of each toggle button in onBindViewHolder.
Something like:
vItem.risposta1.setText(risposta1.getRisposta());
//...
vItem.risposta1.setChecked(risposta1.isChecked());
Also when you update the data in the OnClickListener you have to notify the adapter:
adapter.notifyDataSetChanged();
I'm trying to add a new food with some add ons, basically everytime the add field button is press it will create two edittext which is the name and price of the add on, there will be a scenario whereby users wants to delete the edittext name and price. I've successfully created two edittexts also setting them with the IDs that i'm giving them in integer. But I'm having problem with deleting the two edittext with a button whenever i click the delete field button.
// Add On
addOnLayout.removeAllViews();
addFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddOnCounters++;
etFoodName = new EditText(addfoodAddOn.this);
etFoodName.setId(ETFoodID);
etFoodName.setHint("Enter your AddOn Name");
etPrice = new EditText(addfoodAddOn.this);
etPrice.setId(ETPriceID);
etPrice.setHint("Enter your AddOn Price");
Log.d("-------","-------------------what is edittext name id " + ETFoodID);
Log.d("-------","-------------------what is edittext price id " + ETPriceID);
ETFoodID++;
ETPriceID++;
addOnLayout.addView(etFoodName);
addOnLayout.addView(etPrice);
}
});
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(AddOnCounters>=1 && ETFoodID > 0 && ETPriceID > 10){
addOnLayout.removeViewAt(ETFoodID);
addOnLayout.removeViewAt(ETPriceID);
ETFoodID--;
ETPriceID--;
}
}
});
I'm expecting the result to be whenever i click the delete field button, it will delete the very bottom last two edittexts which is the name and price.
Replace your listener
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(AddOnCounters>=1 && ETFoodID > 0 && ETPriceID > 10){
View v1 = addOnLayout.findViewById(ETFoodID);
View v2 = addOnLayout.findViewById(ETPriceID);
addOnLayout.removeView(v1);
addOnLayout.removeView(v2);
ETFoodID--;
ETPriceID--;
}
}
});
After the button click just do this:
addFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
etPrice.setText("")
etFoodName.setText("");
}
}
Remove it from its parent.
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeFromParent(etFoodName);
removeFromParent(etPrice);
}
});
static void removeFromParent(View view) {
((ViewGroup)view.getParent()).removeView(view);
}
I want to disable a button when the user hits the button five times.
Here's the code
if(mLatestindex<=4) {
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
mFalseButton.setEnabled(true);
mTrueButton.setEnabled(true);
mLatestindex++;
}
}
Your mLatestIndex <= 4 is placed wrongly.
You should check the index inside the Click Listener
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mLatestIndex < 5) {
// Do the stuff
// This will execute only if the index is <= 4
}
mLatestIndex++;
}
});
This will execute the code only if mLatestIndex is less than 5, and increment it.
If you need disable button on 5 times click in general just add global counter and increment it in onClick() with check
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickerCount++;
if (clickerCount >= 5) {
button.setEnabled(false);
}
}
If you need to disable view when it was clicked 5 times in certain amount of time, you could store timestamp of last clicked time and compare it with current time
What I want is when I click the "x" button of the second item, it will remove the second item and then make the "+" of the first item visible. Here is what I try but not work, the "+" button is not showed.
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildAt(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
Try this code,
holder.ivDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = recyclerView.getChildItemId(position - 1);
holder.ivAdd = (ImageView) view.findViewById(R.id.img_add_items);
holder.ivAdd.setVisibility(View.VISIBLE);
list.remove(position);
notifyDataSetChanged();
}
});
I hope that will help you for solving your problem.
Basically I'm new to Android and don't know much about it. I'm making a quiz program in which I'm using custom ListView with 5 custom TextViews, one for question and other 4 for options. My problem is that I want the TextView as clickable as well as the LisView as choice mode as single. That is if I click one text all other TextViews should become unclickable. My problem is whenever I click on a TextView in the child layout, only the outer layout, that is the item of the ListView get selected.
here is the screenshot of the my listview
https://picasaweb.google.com/108429569548433380582/Android?authkey=Gv1sRgCJ3kxJz7tLvaTg#5783846428648608706
You can do it in two ways:
1. Either by directly using onClickListener like this:
textView.setOnClickListener(new View.OnClickListener(
public void onClick(View arg0) {
// Do anything here.
}
});
OR
2. In XML file, in declaration of <TextView /> add one more attribute as:
android:onClick="onClickTextView"
and in yout activity, add this function:
public void onClickTextView(View view) {
// Do anything here.
}
UPDATE:
Use following code to get click event on TextView:
// Click event for single list row
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv = (TextView) (findViewById(R.id.title));
if (tv != null) {
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CLICKED",
Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(MainActivity.this, "TV not found",
Toast.LENGTH_LONG).show();
}
}
});
Try this :
When you select one textview the other three will be unclickable
final TextView texta = (TextView) findViewById(R.id.text_a);
final TextView textb = (TextView) findViewById(R.id.text_b);
final TextView textc = (TextView) findViewById(R.id.text_c);
final TextView textd = (TextView) findViewById(R.id.text_d);
texta.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
textb.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textb.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textc.setClickable(false);
textd.setClickable(false);
}
});
textc.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textd.setClickable(false);
}
});
textd.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
texta.setClickable(false);
textb.setClickable(false);
textc.setClickable(false);
}
});
Assume you extend BaseAdapter to set the listview content ->
Open a TextView listener and settag of the current holder position , and perform your operation in the onclick method.
It's the default behavior of a ListView. Only one could be clickable: either the list row or the items inside the row.
Eg: if the row item is a textView(as in your case) the list row will be clickable but if the row item is a button then the the list row will not be clickable. Same is the case if you make TextView as clickable.
For your requirement the better approach would be to use RadioGroup (instead of multiple text view and disabling and enabling them).
You should use a custom layout for you list item with a TextView for question and a RadioGroup for options.
Layout could be something like this :
Follow these links for reference:
for listView
for RadioGroup
I hope this will help
Thanks for Shrikant and adam for there help Sorry and i appologize for a very late response.
either use this in adapter class as by Shrikant:
textViewa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do anything here.
}
});
textViewb.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Do anything here.
}
});
//and so on...
// or better to use ViewHolder holder; for these type of listviews;
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do what you want to do.
// for my i have to call a method in my parent activity. so in constructor of adapter, I passed the activity and then typecasted it like
ParentActivity parent = (ParentActivity) activity;
parent.chosenAnswer(view.getId());
// then in chosenAnswer(int id) in parentActivity use a switch case for the same logic as in Adam's answer.
// OR
//you can write like this too..
switch (view.getId()) {
case R.id.textViewa:
break;
case R.id.textViewb:
break;
case R.id.textViewc:
break;
case R.id.textViewd:
break;
}
}
};