How do I get the value from EditText and save it in ListView of another Activity. Each time I give the value in EditText it should be saved permanently in the ListView one after the other. Here is my code.
MainActivity where I am retrieving edittext values into listview
public class MainActivity extends Activity {
private ListView lv;
ArrayAdapter<String> adapter;
ArrayList<String> list=new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar=getActionBar();
lv=(ListView)findViewById(R.id.list);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
String item=getIntent().getStringExtra("location");
adapter.add(item);
every time you add a value in EditText along with adding it to list you also need to save it somewhere else(eg.in shared preferences) so that values don't get lost while this activity reinitilizes coz that will reinitilize the list adapter as well. If not compulsary why don't you add values to list in same activity?
Related
My app contains only 2 files one is MainActivity.java and activity_main.xml file. I wanted to use to listView to display a list of string but for some my app is crashing continuously.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Pandey");
arrayList.add("Akshay");
arrayList.add("Akshay");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
ListView listView=(ListView) findViewById(R.id.list_item);
listView.setAdapter(adapter);
}
}
Error
Your app is crashing because you are passing 0 as the second argument to your ArrayAdapter constructor. This is the "layout resource id" that your list will use for each item, so it needs to be something real.
The Android platform comes with some resources for this purpose built in, like android.R.layout.simple_list_item_1. You could try that. Change this line:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
to this instead:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
More information can be found in the developer guide: https://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
You can't set the second argument of ArrayAdapter to 0. The constructor expects a valid resource. You need to set it to something like this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.my_resource_view, arrayList);
I'm kinda new to Android Studio so I will be grateful if you can help me with my question. How can I enter some text and add it to a ListView with the use of a Bundle?
For example, say I enter a name in the EditText component in the MainActivty, and then when I press the OK button, it will be seen in to another Activity in a List View.
I've been using Bundles to transfer text to another Activity but I can't figure out how to transfer text to ListView.
If all you want to do is display strings in your list items, you can do it fairly simply, otherwise you will have to make a custom adapter. For the former, this is what you will want to do
Create an ArrayList<String> where you will store your values that are entered from the EditText.
Create an ArrayAdapter<String> that will serve to connect the ArrayList to the ListView.
Your final code will look something like this:
public class MainActivity extends Activity {
private ListView listView;
List<String> strings;
ArrayAdapter<String> arrayAdapter;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.yourLayout);
listView= (ListView) findViewById(R.id.yourListView);
strings = new ArrayList<String>();
strings.add("list item 1");
strings.add("list item 2");
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
strings);
listView.setAdapter(arrayAdapter);
}
}
Then in the onClick of the "OK" button simply add the string from the EditText to the arrayList, and notify the listView that it should be updated be calling this:
arrayAdapter.notifyDataSetChanged();
I don't think you can use Bundles for what you want to achieve. Here's how I do it...
First you need to set up your list. I would use global variables:
ListView listView;
EditText editText;
ArrayList<String> strings;
ArrayAdapter<String> adapter;
Now, in onCreate() , you can do...
listView = (ListView) findViewById(R.id.your_listview);
editText = (EditText) findViewById(R.id.your_edittext );
strings = new ArrayList<>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
listView.setAdapter(adapter);
You will need to set the OnClickListener for your Button. I would do this in onCreate() too:
Button button = (Button) findViewById(R.id.your_button);
button .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here we get the text from the EditText component
String text = editText.getText().toString();
// Now add it to the list
strings.add(text);
// And finally, update the list
adapter.notifyDataSetChanged();
}
});
I have a EditText box so a user can input names and then click the Add button and the name saves to the array playerList and clears the box so another name can be added.
I also have a ListView on the same Activity which will then be populated by the names in the Array playerList. The problem is that the ListView dosen't seem to be populated.
So I tried with a defualt set String teststring which you can see below and that populates the ListView fine. My question is how come it isn't working with the Array playerList maybe its not saving to the Array correctly?
Update just need adapter.notifyDataSetChanged(); adding to refresh the ListView Credit to #Lalit Poptani and #Jave
ArrayList<String> playerList = new ArrayList<String>();
ListView listview;
protected String[] teststring = {"Name 1", "Name 2"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addremove);
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, teststring);
ListView employeeList = (ListView) findViewById(R.id.namelistview);
employeeList.setAdapter(adapter);
Button confirm = (Button) findViewById(R.id.add);
confirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText playername = (EditText) findViewById(R.id.userinput);
String name = playername.getText().toString();
playerList.add(name);
playername.setText("");
}});
To refresh the ListView after you add new data to it you have to call adapter.notifyDataSetChanged(). This is refresh your ListView will new data.
But in your case you are populating your ListView with String[] so it won't work dynamically you will have to give the size of the String[]. So, I will suggest you to populate your ListView with ArrayList itself for adding the content dynamically.
ListAdapter adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1,playerList);
You should call adapter.notifyDataSetChanged() after adding new items to make it update with the new data.
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.
I am developing an android application in which i have to place 5 items with 5 icons in a list.Has anyone implemented it before?
If Yes,Can he help me how to implement it?
Thanks
tushar
you can try from this
Dynamic ListView in Android app
public class ListViewDemo extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
//RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter=0;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}
android.R.layout.simple_list_item_1 is default list items layout supplied by android and >you can use this stock layout for non complex things.
listItems is an array list which holds the data shown in the ListView and all the >insertion and removal should be done on listItems the changes in list should reflect in >the view and thats handled by ArrayAdapter adapter which should be notified using
adapter.notifyDataSetChanged();
Adapter is instantiated with 3 paramters the context which could be your >activity/listactivity the layout of you individual list item and lastly the list which is >the actual data to be displayed in the list.
In my activity there is a listview containing lines of a text file. To populate this listview i created an arrayadapter in the onCreate(Bundle savedInstanceState) method. The listview is populated with items if there are lines in the file. The arrayadapter part looks like this:
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
ArrayAdapter<String> adapter1;
adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,R.id.list_content, assignArr0);
lv1.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
Then by clicking on Menu item1 (so now we are outside the onCreate(Bundle savedInstanceStat) method i write one line in the text file. For now everything works fine. If i exit and reopen the app the new line appears in the listview. But the listview is not refreshed after adding the new line, so at the end of the " case R.id.Menu1:" part.
So after adding one line to the file I want to refresh the listview by reading the lines from the file to an array then populating the listview with that array. Problem is that i cannot do that, even if i put the ArrayAdapter<String> adapter1; line outside the onCreate() method where the other arrays and variables are declared.
It does not see the listview (lv1 cannot be resolved).
If i define the ListView where the other variables are (ListView lv1;) this whole part of the program is not working.
I also need to empty the listview (the adapter?) by adapter1.clear(); or sg before populating it.
Any ideas are welcome.
If you need those variables to be global, you should declare them as
private final ArrayList<String> assignArr0 = new ArrayList<String>();
private ListView lv1;
private ArrayAdapter<String> adapter1;
#Override
public void onCreate(Bundle icicle)
{
lv1 = (ListView) findViewById(R.id.ListView01);
adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,
R.id.list_content, assignArr0);
lv1.setAdapter(adapter1);
}
You should set the adapter to the ListView only one time, inside the onCreate method of your activity, and the notifiyDatasetChanged should be called after you made changes to the list (the array used in your ListAdapter).
Say you have a method to add the recently read lines not yet included in your list. After adding the lines to the ArrayList used in the ListAdapter, you should call the notifyDatasetChanged method, to visualize the new items in the ListView:
private void addLinesToList(final String... newLines)
{
assignArr0.addAll(Arrays.asList(newLines));
adapter1.notifyDataSetChanged();
}
If you are reading the whole file every time it changes, and populate it from top (not just inserting the new lines), you have to first remove all the elements of the list, so the first line of the addLinesToList method should be:
assignArr0.clear();