android.R.simple_spinner_adapter cannot be resolved - android

Android 2.3.3
I am having a Spinner in my xml file and I wish to set an ArrayList as a source. I am trying to instantiate the ArrayAdapter with the ArrayList, but i don't get the R.simple_spinner_adapter to select.
Here is the code :::
public class UnitConverter extends Activity{
Spinner spnUnit;
ArrayAdapter<String> ad;
ArrayList<String> alAngle = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.unitconverter);
spnUnit = (Spinner) findViewById(R.id.spinnerUnit);
ad = new ArrayAdapter<String>(this, android.R.simple_spinner_adapter, alAngle);
// Error at above line near android.R.simple_spinner_adapter
setDataToArrayLists();
}

The ans to ur question is u are not using adapter properly:-
android.R.simple_spinner_adapter should be android.R.layout.simple_spinner_item
Sample to this can be:-
Spinner spinner = (Spinner) findViewById(R.id.font_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.font_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
check using as above this is sample used in my code...!!!!

Related

Updating Spinner using SQLite database

I need to constantly update my Spinner after adding data which it shows.
Part of my code:
public class MainActivity extends AppCompatActivity {
ArrayAdapter<String> adapter;
Spinner spinner;
protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> categories = databaseHelper.getAllCategories();
spinner = (Spinner) findViewById(R.id.spinner);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
In DatabaseHelper class, I make simple query which selects one column.
public class MainActivity extends AppCompatActivity {
ArrayAdapter<String> adapter;
Spinner spinner;
ArrayList<String> categories;
protected void onCreate(Bundle savedInstanceState) {
categories = databaseHelper.getAllCategories();
spinner = (Spinner) findViewById(R.id.spinner);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
private function updateSpinner(){
categories.clear();
categories.addAll(databaseHelper.getAllCategories());
adapter.notifyDataSetChanged();
}

Inserting into ArrayAdapter does not update Spinner selection

I am trying to insert an item into an Adapter that multiple Spinners are using. However when I insert into the adapter, I would like the Spinners to retain their original selection based off the object and not the position.
In the case below, the Spinner is originally selecting "four", but when I click the button to insert "three", the spinner is now set to "three" instead of updating to the new position of "four". How can I achieve this?
public class MyActivity extends Activity {
List list;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("four");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, list);
spinner.setAdapter(adapter);
// set selection to "four"
spinner.setSelection(2);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//list.add(2, "three") causes the same problem
adapter.insert("three", 2);
adapter.notifyDataSetChanged();
}
});
}
Should you call spinner.setAdapter(adapter); or a similar method after inserting a new value into the adaptor?
spinner.setSelection(list.indexOf("four"));
This will set the selection to "four" no matter on what position it is.

Null Pointer exception error with spinners int array

So when I try to set dropdown3 as adapter1, I get a null pointer exception. However I do not get any issues with the String arrays. Why is this happening with the Integer array? What can I do to fix it??
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner dropdown1 = (Spinner)findViewById(R.id.spinnertext1);
Spinner dropdown2 = (Spinner)findViewById(R.id.spinnertext2);
Spinner dropdown3 = (Spinner)findViewById(R.id.spinnernumber1);
String[] items = new String[]{"ml", "oz", "L"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
Integer[] numbers = new Integer[]{1,2,3,4,5,6,7,8,9,10};
ArrayAdapter<Integer> adapter1 = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, numbers);
//adapts the string "items" to be an adapter and fit into the spinner.
dropdown1.setAdapter(adapter);
dropdown2.setAdapter(adapter);
dropdown3.setAdapter(adapter1);
I don't think you can use the same adapter object for two different views.
Try instantiating three different adapters.

simple_list_item_1 can not be resolved or not a field

I am beginner in android ,I make a simple array list. and bind it to listview my code is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText textbox1=(EditText)findViewById(R.id.editText1);
ListView listview1=(ListView)findViewById(R.id.listView1);
String[] items={"a1","a2","a3","a4","a5","a6","a7","a8","a9","a10"};
final ArrayList<String> todo=new ArrayList<String>();
final ArrayAdapter<String> aa;
aa=new ArrayAdapter<String>(this, R.layout.simple_list_item_1,items);
listview1.setAdapter(aa);
}
I am getting error in simple_list_item_1 I search on google but all the exmples tell only way to bind not why this error comes.
Change
aa=new ArrayAdapter<String>(this, R.layout.simple_list_item_1,items);
to
aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
It's currently trying to take the value of layout.simple_list_item_1 from your app's R class.

how to add item to Spinner's ArrayAdapter?

i had a EditText , a button and a spinner . When click the button , the spinner will add a new item with name you entered in the EditText. But here is the question, my adapter.add() method seems doesn't work...here is my code:
public class Spr extends Activity {
Button bt1;
EditText et;
ArrayAdapter<CharSequence> adapter;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt1 = (Button)this.findViewById(R.id.bt1);
et = (EditText)this.findViewById(R.id.et);
spinner = (Spinner)this.findViewById(R.id.spr);
adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String temp = et.getText().toString();
adapter.add(temp);
adapter.notifyDataSetChanged();
spinner.setAdapter(adapter);
}
});
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}});
}
}
thanks! ...still waitting
When you have created your ArrayAdapter you haven't assigned a resizeable List to it, so when you do add() it cannot increment the size of it and throws a UnsupportedOperationException.
Try something like this:
List<CharSequence> planets = new ArrayList<CharSequence>();
adapter = new ArrayAdapter<CharSequence>(context,
R.array.planets_array, planets);
//now you can call adapter.add()
You should use a List. With an Array such as CharSequence[] you would get the same UnsupportedOperationException exception.
Javi is right except don't reference an array for the second parameter.
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item,
someList);
I believe this is working as designed, but not as expected. ArrayAdapter used to only take an array, but the list constructor was added later. I'm guessing its just doing a toArray() on your list. This is why you have to either call add on the adapter, or create a new adapter when your List changes.
you can create an arraylist and copy all recourse to this object then create arrayadaptor and send this arraylist and in onclicklistener of button, add edittext content to arraylist object then call notifydatasetchanged of adator

Categories

Resources