Reputation: 13
I am attempting to take Input Text from a EditText
field, save the String
and then add/save it to an ArrayList
I have in another java class
. (My MainActivity
displays all the facts
I have stored in the ArrayList
, which works.) Adding new String
to ArrayList
from EditText does not work though.
Thanks for the help!
FactBook.java
public class FactBook {
public ArrayList<String> mFacts = new ArrayList<String>(Arrays.asList(
"Quote 1",
"Quote 2"));
public String getFact() {
String fact = "";
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFacts.size());
fact = mFacts.get(randomNumber);
return fact;
}
public void addFact(String thought) {
mFacts.add(thought);
};
InputActivity.java
public class InputActivity extends AppCompatActivity {
private Button mSaveButton;
private EditText mTextThought;
private FactBook mFactBook = new FactBook();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSaveButton = (Button) findViewById(R.id.saveButton);
mTextThought = (EditText) findViewById(R.id.thoughtText);
View.OnClickListener click = new View.OnClickListener() {
@Override
public void onClick(View view) {
String thought = mTextThought.getText().toString().trim();
mFactBook.addFact(thought);
Toast.makeText(InputActivity.this, "Your thought has been added!!",
Toast.LENGTH_LONG).show();
mTextThought.setText("");
}
};
mSaveButton.setOnClickListener(click);
}
}
Upvotes: 0
Views: 916
Reputation: 6828
You addFact
method should be,
public void addFact(String thought) {
this.mFacts.add(thought);
}
Upvotes: 0
Reputation: 1
public void addFact(String thought) {
this.addFact(thought);
maybe you should change your code like this:
public void addFact(String thought) {
mFacts.addFact(thought);}
Upvotes: 0
Reputation: 837
I think there is a problem with your code. Shouldn't the addFact code be this :
public void addFact(String thought) {
mFacts.addFact(thought);
};
Upvotes: 3