Android:Fill Spinner from Java Code Programmatically

Android : Fill Spinner From Java Code Programmatically

// you need to have a list of data that you want the spinner to display
List<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("item1");
spinnerArray.add("item2");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner sItems = (Spinner) findViewById(R.id.spinner1);
sItems.setAdapter(adapter);

also to find out what is selected you could do something like this

String selected = sItems.getSelectedItem().toString();
if (selected.equals("what ever the option was")) {
}

How to programatically set entries of spinner in android?

You need to use an adapter and populate with tha array in xml file.

Specify the name of your array in xml at createFromResource method (second parameter).

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.my_array, android.R.layout.simple_spinner_item);   
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);

How to populate a Spinner from String array

Use ArrayAdapter this way

your string array

   String[] test=new String[]{"test1","test2"};

your ArrayAdapter

   ArrayAdapter<String> gameKindArray= new ArrayAdapter<String>(MyActivityClass.this,android.R.layout.simple_spinner_item, test);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);

Android: fill a spinner data from the server dynamically

Each item in spinner indicates one "WayPoint". So first step is create model class for WayPoint.

class WayPoint {
String name;
Pose pose;

@NonNull
@Override
public String toString() {
return name;
}
}

class Pose {
Position position;
Orientation orientation;
}

class Position {
double x, y, z;
}

class Orientation {
double x, y, z, w;
}

Second, write a method to parse a json string to way point list

private List<WayPoint> getWayPointListFromJsonString(String json) {
List<WayPoint> wayPoints = new ArrayList<>();

try {
JSONObject jsonObject = new JSONObject(json);
JSONArray wayPointJsonArray = jsonObject.getJSONObject("msg").getJSONArray("waypoints");
for (int i = 0; i < wayPointJsonArray.length(); i++) {
JSONObject wayPointJsonObject = wayPointJsonArray.getJSONObject(i);

// Parse name
String name = wayPointJsonObject.getString("name");

// Parse pose to get position and orientation;
JSONObject poseJsonObject = wayPointJsonObject.getJSONObject("pose");

// Parse position form pose
JSONObject positionJsonObject = poseJsonObject.getJSONObject("position");
Position position = new Position();
position.x = positionJsonObject.getDouble("x");
position.y = positionJsonObject.getDouble("y");
position.z = positionJsonObject.getDouble("z");

// Parse orientation from pose.
JSONObject orientationJsonObject = poseJsonObject.getJSONObject("orientation");
Orientation orientation = new Orientation();
orientation.x = orientationJsonObject.getDouble("x");
orientation.y = orientationJsonObject.getDouble("y");
orientation.z = orientationJsonObject.getDouble("z");
orientation.w = orientationJsonObject.getDouble("w");

Pose pose = new Pose();
pose.position = position;
pose.orientation = orientation;

WayPoint wayPoint = new WayPoint();
wayPoint.name = name;
wayPoint.pose = pose;
wayPoints.add(wayPoint);
}
} catch (JSONException e) {
e.printStackTrace();
}

return wayPoints;
}

Finally put them into MainActivity class.

public class MainActivity extends AppCompatActivity {

private Spinner wayPointSpinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wayPointSpinner = findViewById(R.id.spinner_way_point);

// This is the json string returned from server.
String jsonString = "{\"topic\": \"/waypoints\", \"msg\": {\"waypoints\": [{\"header\": {\"stamp\": {\"secs\": 0, \"nsecs\": 0}, \"frame_id\": \"map\", \"seq\": 0}, \"pose\": {\"position\": {\"y\": -140.901899144836, \"x\": 44.36469369653882, \"z\": 0.0}, \"orientation\": {\"y\": 0.0, \"x\": 0.0, \"z\": 0.5062775131960053, \"w\": 0.8623706161692133}}, \"name\": \"A\"}, {\"header\": {\"stamp\": {\"secs\": 0, \"nsecs\": 0}, \"frame_id\": \"map\", \"seq\": 0}, \"pose\": {\"position\": {\"y\": -102.30524463734227, \"x\": 51.282396271771304, \"z\": 0.0}, \"orientation\": {\"y\": 0.0, \"x\": 0.0, \"z\": 0.6926372547579506, \"w\": 0.721286096719859}}, \"name\": \"B\"}]}, \"op\": \"publish\"}";

// This will convert the json string to list of WayPoint
List<WayPoint> wayPoints = getWayPointListFromJsonString(jsonString);

// Create adapter for spinner
SpinnerAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, wayPoints);

wayPointSpinner.setAdapter(adapter);
wayPointSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int p, long id) {
WayPoint wayPoint = (WayPoint) parent.getItemAtPosition(p);

// Process position here
Position position = wayPoint.pose.position;

// Process orientation here
Orientation orientation = wayPoint.pose.orientation;
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

Underline Spinner Created Programmatically

Set your style as R.style.Widget_AppCompat_Spinner_Underlined

Mode = Spinner.MODE_DROPDOWN | Spinner.MODE_DIALOG

new Spinner(this, null, 0, R.style.Widget_AppCompat_Spinner_Underlined, Spinner.MODE_DROPDOWN)

Android: Create spinner programmatically from array

ArrayAdapter<String> should work.

i.e.:

Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item,
spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

Create a spinner programmatically in Android

You need to add the Spinner to a layout.

First create a container for the Spinner and then create the Spinner and add it to your container. Next set content of you Activity to your container.

This could be done like this, in your onCreate method:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

LinearLayout layout = new LinearLayout(this);

ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");

Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);

layout.addView(spinner);

setContentView(layout);
}

EDIT:

Just to clarify: if the Spinner isn't added to the content of the Activity inside a layout, it isn't visible, so that's why you don't get any errors or anything, because there isn't any errors in your code, per se ;-)



Related Topics



Leave a reply



Submit