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.
Related
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.
I have a dummy listview that I have setup in my app that uses the following code:
public class Roster extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roster);
// ListView resource.
mainListView = (ListView) findViewById( R.id.rosterListView );
// List of names.
String[] players = new String[] { "Dude1", "Guy2"};
ArrayList<String> playerlist = new ArrayList<String>();
playerlist.addAll( Arrays.asList(players) );
//ArrayAdapter using the list.
listAdapter = new ArrayAdapter<String>(this, R.layout.rosterrow, playerlist);
// Add more Players, If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Person8" );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
getActionBar().setDisplayHomeAsUpEnabled(true);
}
which gives me a listview of three players.
I now want to use the json object to populate the list instead of using a string. How can I populate the list using my json object?
Try to get the value of Json by key and put it in to string array.
JsonObject myjsondata = new JsonObject(jsondata.toString());
String[] players = new String[] {myjsondata.getString("key") , myjsondata.getString("key1")};
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.
Once a value is selected from the spinner, I am looking to have another list of values populate the spinner from the selection made. For example: When the user clicks the spinner the values "Home Team, Home Subs, Home Other" comes up. The user clicks one of these and then the players that are affiliated with that selection then populate the spinner. Below is code for the original spinners populated.
public class ExampleMain extends Activity {
JSONArray jsonArray = null;
JSONArray str_login = null;
public String kode;
public String Team_Name;
public String Home_team;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
JSONParser jParser = new JSONParser();
String link_url = "http://10.0.2.2/NPD/P_name.php";
JSONObject json = jParser.FunctionParser(link_url);
Spinner d11 = (Spinner)findViewById(R.id.doubles11);
Spinner d12 = (Spinner)findViewById(R.id.doubles12);
Spinner d21 = (Spinner)findViewById(R.id.doubles21);
Spinner d22 = (Spinner)findViewById(R.id.doubles22);
Spinner d31 = (Spinner)findViewById(R.id.doubles31);
Spinner d32 = (Spinner)findViewById(R.id.doubles32);
try {
jsonArray = json.getJSONArray("team");
final String[] items = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items[i]=jsonObject.getString("P_name");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d11.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d12.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d21.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d22.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d31.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d32.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
Thanks.
You need to use OnItemSelectedListener
d11.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{
item = spinner.getItemAtPosition(position).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
Now you will have your value and you can use that wherever you need to populate the other Spinners. Make item in this example a member variable so you can use it anywhere in this Activity. You will also need to add implements OnItemSelectedListener to your class definition
public class ExampleMain extends Activity implements OnItemSelectedListener{
Also, you are initializing adapter many times with the same values it looks like. Just initialize it once then set your Adapters on your Spinners
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...!!!!