I'm working on an App and need some help with the spinner widget.
The array has no entries by its creation and will be filled in another function. So I have to use adapter.add(item); and spinner.setAdapter(adapter); to update the spinner in this function, I guess.
// global variables
String[] test = new String[100];
int x = 0;
Spinner s;
ArrayAdapter adapter;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
s = (Spinner) findViewById(R.id.spinner1);
adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, test);
}
public void onClick(View arg0) {
test[x] = String.valueOf(x);;
x++;
adapter.add(test[x]);
s.setAdapter(adapter);
}
just for education purpose I tried this, but there is still a bug in the onClick() function.
Something has to be done with the ArrayAdapter, I guess. But I don't familiar with this and didn't found an example for adding itmes by pressing a button.
Maybe there is just a simple failure somewhere else, but currently I just don't see it.
here the complete code:
ArrayList<Integer> test;
int x = 0;
Spinner s;
ArrayAdapter adapter;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
s = (Spinner) findViewById(R.id.spinner1);
test = new ArrayList<Integer>();
adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, test);
}
public void onClick(View arg0) {
test.add(x);
x++;
s.setAdapter(adapter);
}
You are adding x to an array element then passing this element to the adapter.add method. This is effectively the same as using: adapter.add(x);.
Why not use an ArrayList instead of a array. ArrayLists are dynamic, and do not need to be given an initial size.
A very quick check has shown me that there is a constructor for an Array Adapter that is compatible with ArrayLists and I expect any descendant of the List class. See the code example below.
ArrayList<Integer> test = new ArrayList<Integer>();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, test);
I know this is not a complete solution to your problem, but just a pointer to a possibly better way of doing it.
Do a web search with the terms ‘android updating arrayAdapter at runtime’ and I am sure you will stumble upon a more concrete example.
The problem is you are setting the ArrayAdapter's data to be a 100 null strings, and then adding to the end of that when you press the button. Try changing your test variable to be new String[0].
Then in your onClick method, just call adapter.add(String). It will add it to the end of your current data, so you don't need to give it a big enough array initially.
Related
I'm trying to make a list containing names. This list should be modifiable (add, delete, sort, etc). However, whenever I tried to change the items in the ArrayAdapter, the program crashed, with java.lang.UnsupportedOperationException error. Here is my code:
ListView panel = (ListView) findViewById(R.id.panel);
String[] array = {"a","b","c","d","e","f","g"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, array);
adapter.setNotifyOnChange(true);
panel.setAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
adapter.insert("h", 7);
}
});
I tried insert, remove and clear methods, and none of them worked. Would someone tell me what I did wrong?
I tried it out myself and found it didn't work, so I checked out the source code of ArrayAdapter and found out the problem: The ArrayAdapter, on being initialized by an array, converts the array into an AbstractList (List<String>) which cannot be modified.
Solution
Use an ArrayList<String> instead using an array while initializing the ArrayAdapter.
String[] array = {"a","b","c","d","e","f","g"};
ArrayList<String> lst = new ArrayList<String>(Arrays.asList(array));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lst);
ok so I'm making an app where I use an edit text to write anything and when I press a button it adds whatever I wrote in the edit to a listView, and it works, but I want the List to be in a different activity than the button and edit text, so I moved it without changing the code.can anyone figure this out,
BTW all the variables are public.
public class MainActivity extends AppCompatActivity {
public ArrayList<String> arrayList2;
public ArrayAdapter<String> adapter,adapter2;
public EditText editText,editText2;
public ArrayList<String> itemList,itemList2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {};
itemList = new ArrayList<String>(Arrays.asList(items));
adapter = new ArrayAdapter<String>(this, R.layout.activity_list_layout,R.id.txtItem,itemList);
ListView listV = (ListView)findViewById(R.id.list_layout);
listV.setAdapter(adapter);
editText = (EditText)findViewById(R.id.thingadd);
Button btAdd = (Button)findViewById(R.id.add);
String[] age = {};
arrayList2 = new ArrayList<String>(Arrays.asList(age));
itemList2 = new ArrayList<String>(Arrays.asList(age));
adapter2 = new ArrayAdapter<String>(this, R.layout.activity_list__layout2,R.id.txtage,itemList2);
ListView listV2 = (ListView)findViewById(R.id.Age);
listV2.setAdapter(adapter2);
editText2 = (EditText)findViewById(R.id.agetxt);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newItem=editText.getText().toString();
String newItem2=editText2.getText().toString();
itemList.add(newItem);
itemList2.add(newItem2);
adapter.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
});
}
}
You can use SQLite Database for that, where you would be saving data in database from one activity and reading it and displaying it in listview from another.
You have 2 options to do so.
You can use any database service. Store the values and display them in the next activity. But this needs Internet service to be enabled in the phone.
You can use intent. Convert those input to string type and pass them to next activity and display them using ids. This type do not require Internet service. To know more about this, Visit this link
I'm working on a program that uses a ListView which contains strings that a user enters. I want it to list out each string after the user clicks the button. Also I want every string value that is displayed in the ListView to also be in an array so that I can manipulate the list later. This is what I have right now:
public ArrayList<String> choices = new ArrayList<String>();
public String[] choicesArray;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView1);
choicesArray = new String[] { "You have not entered a choice yet" };
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
choicesArray);
listView.setAdapter(adapter);
final Button addButton = (Button) findViewById(R.id.Button1);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
adapter.add(message);
//choices.add(message);
//choicesArray = choices.toArray(choicesArray);
//listView.setAdapter(adapter);
}
});
}
This code creates an error:
FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:404)
at java.util.AbstractList.add(AbstractList.java:425)
at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
at MainActivity$1.onClick(MainActivity.java:38)
The reason your list is not updating is that you are not ever adding your data items to the Adapter. You are only adding them to choicesArray which is the array that you passed to the new ArrayAdapter() constructor.
That constructor does not tie the adapter to the array, but basically copies the array into the adapter (at the time that you call the constructor) Changes made to the array after that will not be reflected in the ArrayAdapter.
I like to think of it that ArrayAdapter<> is basically the same thing as ArrayList<> with the added bonus of being able to generate child views for some AdpaterView parent. You can pass it an array[] to start with and it will initialize itself with the values in that array, but it does not link the array to the to the Adapter, or to the ListView. After that initial constructor call any changes to either one will not be reflected in the other.
So you should change your code to look like this:
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
adapter.add(message);
//adapter.notifyDataSetChanged(); //<-- you might need to call this, but I think it might work without if you do it this way.
//listView.setAdapter(adapter); //<-- you shouldn't need to call this each time. Once at the begining is enough.
}
});
Edit:
change your ArrayAdapter constructor and remove the choiceArray parameter so you should be left with this:
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1);
after that I think you should be good to go. Essentially the problem was that since you were passing in an array, and since arrays are of a fixed size (yours was length = 1) it was not allowing you to add anything. I suspect you could probably use an ArrayList<String> instead of String[] if you wanted, but honestly for your situation I think it will be easier to just skip that parameter all together and add items directly to your adapter whenever you need to add new ones.
after adding the new String to your list you just use notifyDataSetChanged() of your adapter which tells the adapter to update the display and it'll update the ListView with the new string
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
choicesArray = choices.toArray(choicesArray);
adapter.notifyDataSetChanged();
}
});
Update
you should consider making this change too
choicesArray = new String[choices.size()];
choicesArray = choices.toArray(choicesArray);
My senior project group is developing an android application and we would like to be able to implement something like the example found in this screenshot:
What we have right now is a Spinner drop down, the very first default will be "Add faculty...". Once you create that faculty, it will be selectable in the spinner, but in case a class has more than one faculty(ie: Professor, Teaching Assistant), we want to be able to add more than one spinner using the +/- buttons like you see here.
Any examples or leads in the right direction would be most appreciated.
Create two variable of ArrayAdapter and Arraylist of string to be global.
ArrayList<String> art;
ArrayAdapter<String> adapter;
And then in onCreate initialize the spinner as below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
s1 = (Spinner) findViewById(R.id.spinner);
art=new ArrayList<String>();
art.add("professer");
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item,art);//Items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
}
And then in button click , add new elements in array and notify the adapter
public void btn_click(View v)
{
art.add("Technical Assistant");
adapter.notifyDataSetChanged();
}
I'm trying to make a list containing names. This list should be modifiable (add, delete, sort, etc). However, whenever I tried to change the items in the ArrayAdapter, the program crashed, with java.lang.UnsupportedOperationException error. Here is my code:
ListView panel = (ListView) findViewById(R.id.panel);
String[] array = {"a","b","c","d","e","f","g"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, array);
adapter.setNotifyOnChange(true);
panel.setAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
adapter.insert("h", 7);
}
});
I tried insert, remove and clear methods, and none of them worked. Would someone tell me what I did wrong?
I tried it out myself and found it didn't work, so I checked out the source code of ArrayAdapter and found out the problem: The ArrayAdapter, on being initialized by an array, converts the array into an AbstractList (List<String>) which cannot be modified.
Solution
Use an ArrayList<String> instead using an array while initializing the ArrayAdapter.
String[] array = {"a","b","c","d","e","f","g"};
ArrayList<String> lst = new ArrayList<String>(Arrays.asList(array));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lst);