Android Edittext listviews?
I have been scouring the Internet for a way to use an input box at the top of the page, and have an add & remove button.
I want the EditText
to write to the ListView
, which in turn writes the list to a .txt
-file.
I have tried breaking this down into multiple small sections of just simple things li开发者_运维百科ke
- getting the input from the
EditText
to post to aTextView
. - Organizing a
ListView
- writing to a file and
- reading from a file.
If you have any advice about how to get the ListView
to go from the EditText
greatly helpful.
The best way to do this is to have an ArrayList<String>
to store your Strings from the TextView, and in turn have that ArrayList<String>
feed into the ListView
. For example, for your Add button handler:
// Declare our variables
ArrayList<String> myStringList = new ArrayList<String>();
// OnClickListener stuff for Add button
..
myStringList.add(myEditText.getText().toString());
myListView.setListAdapter(new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, myStringList));
..
// Close OnClickListener stuff
Make sure you refresh your list adapter afterwards to show the new values. Likewise if you're deleting, use myStringList.remove(index)
to remove that string from the list and refresh the array adapter. As for writing to a text file, have a look at this thread.
精彩评论