I have a simple app with 50 articles, my previous and next button work good in all choices BUT when the user goes to the end (at 50th article) the app can't load again from the beginning the 1st article.
Thanks for any help, here is my code:
I don't know if needs more cede for to be clear, cause I am a newbie.
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Related
I have made an Android Studio app that plays an mp3 file when you click a button. Suddenly it started crashing. I have checked the code myself and I cannot spot any errors. I tried reinstalling the emulator too and using a physical android device to connect, but still the same issue happens. Here is the java file:
public class MainActivity extends AppCompatActivity {
private Button yes;
private Button no;
private ImageButton iwant;
private ImageButton food;
private ImageButton restroom;
private ImageButton water;
private ImageButton playground;
private ImageButton rice;
private ImageButton school;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
iwant = findViewById(R.id.iwant);
food = findViewById(R.id.food);
restroom = findViewById(R.id.restroom);
water = findViewById(R.id.water);
playground = findViewById(R.id.playground);
rice = findViewById(R.id.rice);
school = findViewById(R.id.school);
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.yes);
final MediaPlayer mediaPlayer2 = MediaPlayer.create(this,R.raw.no);
final MediaPlayer mediaPlayer3 = MediaPlayer.create(this,R.raw.iwant);
final MediaPlayer mediaPlayer4 = MediaPlayer.create(this,R.raw.food);
final MediaPlayer mediaPlayer5 = MediaPlayer.create(this,R.raw.restroom);
final MediaPlayer mediaPlayer6 = MediaPlayer.create(this,R.raw.water);
final MediaPlayer mediaPlayer7 = MediaPlayer.create(this,R.raw.playground);
final MediaPlayer mediaPlayer8 = MediaPlayer.create(this,R.raw.rice);
final MediaPlayer mediaPlayer9 = MediaPlayer.create(this,R.raw.school);
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start();
}
});
no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer2.start();
}
});
iwant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer3.start();
}
});
food.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer4.start();
}
});
restroom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer5.start();
}
});
water.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer6.start();
}
});
playground.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer7.start();
}
});
rice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer8.start();
}
});
school.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer9.start();
}
});
}
}
#RequiresApi(api = Build.VERSION_CODES.O)
public class MainActivity extends AppCompatActivity {
TextSwitcher textSwitcherage, textSwitcherGender;
Button nextButton, previousButton, nextButton2, previousButton2;
String[] age = {"U-4","Kid","Teen","Adult","Old"};
String[] gender = {"Male","Female","Other"};
TextView textView;
Typeface typeface;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
defVars();
}
public void defVars()
{
textSwitcherGender = findViewById(R.id.textSwitcherGender);
textSwitcherage = findViewById(R.id.textSwitcherage);
nextButton = findViewById(R.id.nextButton1);
nextButton2 = findViewById(R.id.nextButton2);
previousButton = findViewById(R.id.previousButton1);
previousButton2 = findViewById(R.id.previousButton2);
typeface = getResources().getFont(R.font.lettersforlearners);
nextAndPreviousButtons(nextButton,previousButton,textSwitcherage,age,0);
nextAndPreviousButtons(nextButton2, previousButton2, textSwitcherGender, gender,0);
}
public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious, final TextSwitcher textSwitcher, final String[] data, final Integer stringIndex)
{
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(stringIndex == data.length - 1)
{
stringIndex = 0;
textSwitcher.setText(data[stringIndex]);
}
else
{
textSwitcher.setText(data[++stringIndex]);
}
}
});
buttonPrevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(stringIndex==0)
{
stringIndex = data.length-1;
textSwitcher.setText(data[stringIndex]);
}
else
{
textSwitcher.setText(data[--stringIndex]);
}
}
});
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
textView = new TextView(MainActivity.this);
textView.setTypeface(typeface);
textView.setTextColor(Color.BLACK);
textView.setTextSize(40);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
return textView;
}
});
textSwitcher.setText(data[stringIndex]);
}
}
I have an error:
"error: cannot assign a value to final variable stringIndex"
I set the stringIndex final but still got that problem. I don't know. Am i have to define Integer variables for stringIndex? Or should I delete the stringIndex final? I need to define separate stringIndex variables for each function
if you declare final Integer stringIndex... then you are done! that is the idea and meaning of the reserved word final... is an object you cannot re assign after...
public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious,
final TextSwitcher textSwitcher, final String[] data, Integer stringIndex)
You don't have to modify stringIndex
public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious, final TextSwitcher textSwitcher, final String[] data, final Integer stringIndex)
{
buttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(stringIndex == data.length - 1)
{
textSwitcher.setText(data[0]);
}
else
{
textSwitcher.setText(data[stringIndex+1]);
}
}
});
buttonPrevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(stringIndex==0)
{
textSwitcher.setText(data[data.length-1]);
}
else
{
textSwitcher.setText(data[stringIndex-1]);
}
}
});
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
textView = new TextView(MainActivity.this);
textView.setTypeface(typeface);
textView.setTextColor(Color.BLACK);
textView.setTextSize(40);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
return textView;
}
});
textSwitcher.setText(data[stringIndex]);
I am trying to pass some data from one activity to another and I have made a toast in the other activity to see if data is being passed.When the toast shows up it is blank.I have done everything right and tried many different times with different methods I have also created another app but the problem still stays.It gives me null.
My java code in the first activity
public class titleandcuisine extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TITLE_GET = "title";
public static final String CUISINE_GET = "cuisine";
ImageView imageView;
Button button;
Spinner spinner;
EditText dish;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titleandcuisine);
dish = findViewById(R.id.editText);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.cuisines,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
button = findViewById(R.id.next);
imageView = findViewById(R.id.close);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), homepage.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = dish.getText().toString();
String text2 = spinner.getSelectedItem().toString();
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(TITLE_GET,text);
data.putExtra(CUISINE_GET,text2);
startActivity(data);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Second activity java code
public class reviewactivity extends AppCompatActivity {
TextView cuisine,title;
ImageView displayimage,back;
Uri myUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviewactivity);
Intent intent = getIntent();
String titleget = intent.getStringExtra(titleandcuisine.TITLE_GET);
String cuisineget = intent.getStringExtra(titleandcuisine.CUISINE_GET);
Toast.makeText(this, titleget + cuisineget, Toast.LENGTH_SHORT).show();
cuisine = findViewById(R.id.reviewcuisine);
title = findViewById(R.id.reviewtitle);
back = findViewById(R.id.backtopostvideo);
displayimage = findViewById(R.id.reviewdisplaimage);
// cuisine.setText(cuisineget);
// title.setText(titleget);
// myUri = Uri.parse(uri);
displayimage.setImageURI(myUri);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),videopost.class));
}
});
}
}
You need to read the text from the EditText when the button is clicked. Something like:
title = dish.getText().toString(); // Add this
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(Intent.EXTRA_TEXT, title);
...
One more thing: You are trying to read the Intent.EXTRA_TEXT at the reviewactivity activity. However, nothing is starting that activity (at least within the piece of code that you shared).
I'v created a list of strings which I choose randomly to display, and so I want to make it that it won't repeat its self. How can I do that.
public class qustionsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qustions);
final String[] questions = getResources().getStringArray(R.array.coupleQuestions);
final String randomQuestions = questions[new Random().nextInt(questions.length)];
final TextView theQuestion = findViewById(R.id.theQustion);
theQuestion.setText(randomQuestions);
Button nextQuestion = findViewById(R.id.nextQuestion);
nextQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randomQuestions = questions[new Random().nextInt(questions.length)];
theQuestion.setText(randomQuestions);
}
});
}
The reason that I have two "randomQuestions" string is because I want a question to display as soon as you get into the activity and that on the click of a button it will randomise a new string.
Instead of everytime randomizing the pick, you can randomly shuffle the questions and sequentially take the next (random) question.
private final List<String> randomQuestions;
private int questionI;
// Cycling endlessly
private String nextQuestion() {
String question = randomQuestions.get(questionI);
++questionI;
if (questionI > randomQuestions.size()) {
questionI = 0;
}
}
// Once through the list:
private String nextQuestion() {
return randomQuestions.remove(0); // Always take the first, removing it.
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qustions);
final String[] questions = getResources().getStringArray(R.array.coupleQuestions);
randomQuestions = Collections.shuffle(Arrays.asList(questions));
Maybe you can create a list where you put the list of existing number like :
public class qustionsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qustions);
int a=new Random().nextInt(questions.length);
final String[] questions = getResources().getStringArray(R.array.coupleQuestions);
ArrayList<Integer> existingNumber=new ArrayList<>();
final String randomQuestions = questions[a];
existingNumber.add(a);
final TextView theQuestion = findViewById(R.id.theQustion);
theQuestion.setText(randomQuestions);
Button nextQuestion = findViewById(R.id.nextQuestion);
nextQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
do{
a=new Random().nextInt(questions.length);
}while(!existingNumber.add(a))
String randomQuestions = questions[a];
theQuestion.setText(randomQuestions);
}
});
}
public class qustionsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qustions);
final String[] questions = getResources().getStringArray(R.array.coupleQuestions);
final int index = new Random().nextInt(questions.length);
final String randomQuestions = questions[index];
questions[index] = questions[questions.length-1];
final TextView theQuestion = findViewById(R.id.theQustion);
theQuestion.setText(randomQuestions);
Button nextQuestion = findViewById(R.id.nextQuestion);
nextQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randomQuestions = questions[new Random().nextInt(questions.length-1)];
theQuestion.setText(randomQuestions);
}
});
}
Use ArrayList to store your questions and after every retrieval of a question from a random index value, remove the same from ArrayList
Below is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
metadata = getIntent().getStringExtra("metadata");
final ArrayList<String> questions = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.questions)));
int i=new Random().nextInt(questions.size());
final String randomQuestions = questions.get(i);
questions.remove(i);
final TextView theQuestion = findViewById(R.id.theQustion);
theQuestion.setText(randomQuestions);
Button nextQuestion = findViewById(R.id.nextQuestion);
nextQuestion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(questions.size()==0) //To make sure we don't access the empty ArrayList
{
theQuestion.setText("End of questions");
return;
}
int i=new Random().nextInt(questions.size());
String randomQuestions =questions.get(i);
questions.remove(i);
theQuestion.setText(randomQuestions);
}
});
}
I want to let the data to be shown on the edittext first. How can I do it? I've already passed the data to the modify page, and I want to let the data first show on the edittext, then let the user change the title if the user needs to change.
Here are my modify page code:
public class MeTodolistModify extends AppCompatActivity implements View.OnClickListener {
private String student_id,student_name,title,input;
private EditText addtext;
private Button change;
private int position;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_todolist_modify);
Bundle bundle = getIntent().getExtras();
title = bundle.getString("title");
position = bundle.getInt("position");
student_id = bundle.getString("student_id");
student_name = bundle.getString("student_name");
createDetail();
}
private void createDetail(){
final FirebaseDatabase db = FirebaseDatabase.getInstance();
final DatabaseReference ref = db.getReference("Student").child(student_id).child("event");
Log.e("MOD",String.valueOf(position));
Log.e("MOD2",title);
change = findViewById(R.id.change);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText addtext = findViewById(R.id.addtext);
input = addtext.getText().toString();
ref.child(Integer.toString(position)).setValue(input);
Intent intent;
switch (v.getId()) {
case R.id.change:
intent = new Intent(MeTodolistModify.this, MeTodolist.class);
intent.putExtra("student_id", student_id );
intent.putExtra("student_name",student_name);
startActivity(intent);
break;
}
}
});
}
#Override
public void onClick(View v) {
}
}
The update code for modify page:
public class MeTodolistModify extends AppCompatActivity implements View.OnClickListener {
private String student_id,student_name,title,input;
private EditText addtext;
private Button change;
private int position;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_todolist_modify);
Bundle bundle = getIntent().getExtras();
title = bundle.getString("title");
position = bundle.getInt("position");
student_id = bundle.getString("student_id");
student_name = bundle.getString("student_name");
createDetail();
}
private void createDetail(){
final FirebaseDatabase db = FirebaseDatabase.getInstance();
final DatabaseReference ref = db.getReference("Student").child(student_id).child("event");
Log.e("MOD",String.valueOf(position));
Log.e("MOD2",title);
change = findViewById(R.id.change);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText addtext = findViewById(R.id.addtext);
addtext.setText(title);
input = addtext.getText().toString();
ref.child(Integer.toString(position)).setValue(input);
Intent intent;
switch (v.getId()) {
case R.id.change:
intent = new Intent(MeTodolistModify.this, MeTodolist.class);
intent.putExtra("student_id", student_id );
intent.putExtra("student_name",student_name);
startActivity(intent);
break;
}
}
});
}
#Override
public void onClick(View v) {
}
}
after getting title from bundle
title = bundle.getString("title");
set title to EditText
addtext.setText(title)
later user can change if he wants
If you want to programmatically set data to EditText
addtext.setText("Some text");