I need to display current values in TextView (after removing String). I'm adding String when button is On and I need to remove it, when it's Off ( I don't know if I do it well), next I need to display Strings in TextView without deleted String. I need to display only Strings from On buttons.I don't know how to display ArrayList in TextView - this code display.setText(mActiveToggles.toString()); doesn't work. Here is my code:
public class Calc extends ActionBarActivity {
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
display = (TextView)findViewById(R.id.textView2);
display.setText("Add item");
}
static boolean isempty=true;
public void changeButton(View sender) {
ToggleButton btn = (ToggleButton) sender;
ArrayList<String> mActiveToggles = new ArrayList<String>();
String b = btn.getText().toString();
boolean on = ((ToggleButton) sender).isChecked();
if(on) {
if (isempty) {
if (b.equals("0")) return;
display.setText(btn.getText());
mActiveToggles.add(b);
isempty = false;
} else {
display.append(btn.getText());
mActiveToggles.add(b);
}
}
else
{
if (b.equals(btn.getText()))
{
mActiveToggles.remove(b);
display.setText(mActiveToggles.toString());
}
}
}
Use a loop. Iterate over each element in the ArrayList to add it to a String (or if you prefer you can use a StringBuilder instead). It would look something like the following:
String activeToggles = "";
for (String s : mActiveToggles) {
activeToggles += s + " ";
}
display.setText(activeToggles);
Related
I try to create my first tiny app but i have a problem.
My tiny math app always say my answer is false.I don't understand why.
This is my first app, i don't know where i'm wrong.
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
Answer always "False"
I'm a new student in the dev world.
The problem with code is that you are comparing string with integer, that's why it always returns false as java is strictly typed language.
problem code:
if(str.equals(result)){...}
possible solutions:
if( str.equals(""+result)){...}
or
str.equals(String.valueof(result)) // best solution
or
if(result==Integer.parseInt(str)){...}
Here is corrected code:
private TextView problem;
private EditText question;
private Button button;
private TextView reponse;
private int aleatoire = new Random().nextInt(61) + 20;
private int aleatoire2 = new Random().nextInt(48) + 20;
private int result = aleatoire + aleatoire2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
problem = (TextView) findViewById(R.id.problem);
question = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
reponse = (TextView)findViewById(R.id.resultat);
problem.setText("Result = "+aleatoire+"+"+aleatoire2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str=question.getText().toString();
if (str.equals(""+result)) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
}
});
As azurefrog comment says, you're comparing a String to an int. You will need to transform str to an int or result to a String. You can for example do:
String str=question.getText().toString();
if (str.equals(String.valueOf(result))) {
reponse.setText("True !");
} else {
reponse.setText("False !");
}
So I am doing a Create Your Own Adventure game/book in for an Android class for a final project. The way I have it set up is with two activities (A menu and a page layout). I have a "Master File" text file that has the relationships between each page (example below). I also have the "pages" setup in text files that need to be retrieved and then set to the TextView and the 3 buttons. (example below of how they are setup.
My question is how am I supposed to retrieve that information correctly.
I want to have it setup that when the Page class does an OnCreate it fills in the textview and the 3 buttons from the correct Page.txt file.
This is my page.class the code for the pages.
public class pages extends AppCompatActivity {
ArrayList<FileRelationship> fileRelationships;
FileRelationship selectedRelationship;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
fileRelationships = new ArrayList<FileRelationship>();
String MasterLine;
InputStream inputStream = this.getResources().openRawResource(R.raw.MasterFile);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
// Creates the arrayList of all the relationships for later use of retreival.
try {
while (( MasterLine = buffreader.readLine()) != null) {
String[] MasterLineArray = MasterLine.split(",");
String filename = MasterLineArray[0];
String choice1 = MasterLineArray[1];
String choice2 = MasterLineArray[2];
String choice3 = MasterLineArray[3];
FileRelationship fr = new FileRelationship(filename, choice1, choice2, choice3);
fileRelationships.add(fr);
}
} catch (IOException e) {
}
TextView storyText = (TextView) findViewById(R.id.storyText);
Button choice1 = (Button) findViewById(R.id.firstchoice);
choice1.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
findRelationship(selectedRelationship.firstLink);
finish();
startActivity(getIntent());
}
});
Button choice2 = (Button) findViewById(R.id.secondchoice);
choice2.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
findRelationship(selectedRelationship.secondLink);
finish();
startActivity(getIntent());
}
});
Button choice3 = (Button) findViewById(R.id.thirdchoice);
choice3.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
findRelationship(selectedRelationship.thirdLink);
finish();
startActivity(getIntent());
}
});
// **THIS IS THAT PART I NEED HELP WITH THE MOST** I dont know what
// to put in the setText part to have it retrieve the correct information.
storyText.setText();
choice1.setText();
choice2.setText();
choice3.setText();
}
public void findRelationship(String nextFileName) {
int pageIndex = fileRelationships.indexOf((new FileRelationship(nextFileName)));
selectedRelationship = fileRelationships.get(pageIndex);
}
}
This is my FileRelationship class, that I use to create the array in my page class.
public class FileRelationship {
String fileName;
String firstLink;
String secondLink;
String thirdLink;
public FileRelationship(String fileName) {
this.fileName = fileName;
}
public FileRelationship(String fileName, String firstLink, String secondLink, String thirdLink) {
this.fileName = fileName;
this.firstLink = firstLink;
this.secondLink = secondLink;
this.thirdLink = thirdLink;
}
public boolean equals(Object o) {
if(o == null) {
return false;
}
if(this == o) {
return true;
}
if(o instanceof FileRelationship) {
return this.fileName.equals(((FileRelationship)o).fileName);
}
return false;
}
}
This is how my MasterFile.txt is setup to define the relationships (Just an example)
Page1,Page2,Page3,Page4
Page2,Page5,Page6,Page7
Page3,Page8,Page9,Page10
And finally this is how my Page1,Page2,Page3 etc. will be setup (Again just an example)
This is a practice story line, my pages text will go into here and create the story in this way.
Choice 1
Choice 2
Choice 3
Is the best way to create a method that retrieves the information from the selectedRelationship and then puts all 4 of them into strings? If so how can this be done. I also am not sure if my FindRelationship method is correct either.
Any help would be appreciated. I feel like I have everything else correct, but I wasn't also sure if I am retrieving the MasterFile parts correctly and putting it into the array list correctly either. Any help would be much appreciated. Thanks!
I'm new here, and also new to Android Studio.
My first project is to create a diary to save a users workouts.
I'm having trouble saving for this data..
Here is my front page for the app. With the button "Styrkeboken" (Strengthbook) I'll be entering my sets, reps, weight(vikt) and excercise(övning). I want to save this data in the second button "Historik" (History), but I can't manage it to work as I want to..
Code from my class "Pass" (Workout):
package com.example.mama0086.styrkeboken;
public class Pass {
private int _set;
private int _reps;
private float _vikt;
private String _övning;
public Pass() {
}
public Pass(int set, int reps, float vikt, String övning) {
this._set = set;
this._reps = reps;
this._vikt = vikt; //Vikt = weight
this._övning = övning; //Övning = excercise
}
public void set_set(int _set) {
this._set = _set;
}
public void set_reps(int _reps) {
this._reps = _reps;
}
public void set_vikt(float _vikt) {
this._vikt = _vikt;
}
public void set_övning(String _övning) {
this._övning = _övning;
}
public int get_set() {
return _set;
}
public int get_reps() {
return _reps;
}
public float get_vikt() {
return _vikt;
}
public String get_övning() {
return _övning;
}
}
Code from my class "Historik" (History):
public class Historik extends AppCompatActivity {
EditText editSet;
TextView textSet;
EditText editReps;
TextView textReps;
EditText editVikt;
TextView textVikt;
EditText editÖvning;
TextView textÖvning;
MyDBHandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_historik);
editSet = (EditText) findViewById(R.id.editSet);
textSet = (TextView) findViewById(R.id.textSet);
editReps = (EditText) findViewById(R.id.editReps);
textReps = (TextView) findViewById(R.id.textReps);
editVikt = (EditText) findViewById(R.id.editVikt);
textVikt = (TextView) findViewById(R.id.textVikt);
editÖvning = (EditText) findViewById(R.id.editÖvning);
textÖvning = (TextView) findViewById(R.id.textÖvning);
dbHandler = new MyDBHandler(this, null, null, 1);
printDatabase();
}
//Add excercise
public void addBtn(View view){
Pass pass = new Pass(textSet.getText().toString()); //Pass is swedish for work-out
dbHandler.addPass(pass);
printDatabase();
}
public void removeBtn(){
String inputText = textSet.getText().toString();
dbHandler.deletePass(inputText);
printDatabase();
}
public void printDatabase(){
String dbString = dbHandler.historikToString();
textSet.setText(dbString);
editSet.setText("");
textReps.setText(dbString);
editReps.setText("");
textVikt.setText(dbString);
editVikt.setText("");
textÖvning.setText(dbString);
editÖvning.setText("");
}
}
In the "Historik" class I get this error message:
"Cannot resolve constructor 'Pass(java.lang.String)'
please let me know if you need the code for my database.
I appreciate the help!
You defined 2 different constructors for your Pass class :
public Pass() {
}
public Pass(int set, int reps, float vikt, String övning) {
this._set = set;
this._reps = reps;
this._vikt = vikt; //Vikt = weight
this._övning = övning; //Övning = excercise
}
However in your Historik activity, you're trying to instantiate a new Pass with a String only as a parameter Pass pass = new Pass(textSet.getText().toString());
You will need to change this line to match one of the 2 constructors you have, or define a 3rd one taking only a String as argument
EDIT : Depending on your Pass object, you can either :
declare a 3rd constructor taking only a String (I assume it's övning)
public Pass(String övning) {
this._övning = övning;
}
if the textSet.getText().toString() is not relevant, simply use the default constructor Pass pass = new Pass();
I want to compare my input string with an array list of another class, so I used the searchfield to compare them. As below it works fine.
Input Class
public class Suche extends Activity{
private Button zumergebnis;
final Intent zum_ergebnis = new Intent("android.intent.action.activity_ergebnis");
static String mystring;
static void setstring(String textView){
mystring = textView;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_suche);
zumergebnis = (Button) findViewById(R.id.zumergebnis);
zumergebnis.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
EditText searchinput = (EditText) findViewById(R.id.editText1);
String from = searchinput.getText().toString();
setstring(from);
startActivity(zum_ergebnis);
}
});}
}
Comparing Class
public class Ergebnis extends Suche
{
private TextView textView;
static String getstring(){
return mystring;
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ergebnis);
textView = (TextView) findViewById(R.id.textView2);
textView.setText(mystring);
class2 Object = new class2();
final ArrayList<String> word = Object.method2();
ListAdapter listenAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, word);
ListView mystringsView = (ListView) findViewById(R.id.listView1);
mystringsView.setAdapter(listenAdapter);
textView = (TextView) findViewById(R.id.textView2);
String searchField = (mystring);
for (String s : word)
{
if (searchField.toString().contains(s))
{
textView.setText("Your word"+mystring+"exists in the list");
}
else
{
textView.setText("Word"+mystring+"doesnt exist");
continue;
}
break;
}
}
}
list class
public ArrayList method2(){
ArrayList<String> word = new ArrayList<String>();
word.add("uno");
word.add("dos");
word.add("tres");
return word;
}
}
but when adding a second:
list class
public ArrayList method2(){
ArrayList<String> word = new ArrayList<String>();
word.add("uno; one");
word.add("dos; two");
word.add("tres; three");
return word;
}
}
'column' the app stops.
Anyone an idea how to even search for the second entry?
For everyone who is interested in the solution for that problem, the for- loop has to be changed to:
for (int i = 0; i < word.size(); i++) {
if (word.get(i).contains(searchField.trim())) {
textView.setText("Your input '" +mystring+ "' exists.");
}
else
adding some entries to the arraylist:
public ArrayList method2(){
ArrayList<String> word = new ArrayList<String>();
word.add("uno; one");
word.add("dos; two");
return word;
}
it works fine now.
Could anyone tell me what is wrong in my code?
I am trying to pass a string to function removeWords() and this function removes some information from the String.
For example if I pass:
"I Have a Headach"
the function should return:
"Headach"
However, my function is not working:
public class WordChosen extends Activity {
private TextView wordsList;
private String symptom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_chosen);
//Getting String from VoiceRecognition Activity and displaying it
Intent intent = getIntent();
String wordChosen = intent.getExtras().getString("wordChosen");
//casting the string with TextView to display the result
wordsList = (TextView) findViewById(R.id.wordChosen);
Log.v("Word List:", "+++++"+wordChosen);
//Setting text to be displayed in the textView
removeWords(wordChosen);
Log.v("removewords:", "------- message is displayed");
}
public void removeWords(String wordList)
{
ArrayList<String> stopList = null;
stopList.add("i");
stopList.add("have");
stopList.add("a");
ArrayList<String> result = new ArrayList<String>(Arrays.asList(wordList.split(" ")));
for(int i=0; i<result.size();i++)
{
for(int j=0; j<stopList.size();j++)
{
if (result.get(i).equals(stopList.get(j))) {
break;
}
else {
if(j==stopList.size()-1)
{
wordsList.setText(result.get(i));
}
}
}
}
}
}
public static void main(String[] args) {
String word = "I Have a Headach";
String remove = "I Have a ";
System.out.println(removeWords(word, remove));
}
public static String removeWords(String word ,String remove) {
return word.replace(remove,"");
}
output : Headach