Reputation: 362
Hi I am new to spinner use, never used it before and also populating it with the json data. I am trying to find some solution or atleast learn few things on how to do that, but not able to get any understandable solution. Would appreciate if someone can guide me on a proper path.
My JSON data looks something like below,
{
"Devices": [
{
"type": "alarm",
"displayType": "Alarm",
"imageId": "alarm"
},
{
"type": "audio_bridge",
"displayType": "Audio Bridge",
"imageId": "audio"
},
{
"type": "av_receiver",
"displayType": "Av Receiver",
"imageId": "default"
},
{
"type": "baby_monitor",
"displayType": "Baby Monitor",
"imageId": "mobile"
},
{
"type": "baseport",
"displayType": "Baseport",
"imageId": "default"
},
{
"type": "camera",
"displayType": "Camera",
"imageId": "camera"
},
{
"type": "console",
"displayType": "Console",
"imageId": "console"
}
]
}
I need to just pull displayType from this JSON. And I have a model class to follow through and fetch whatever data needed as below,
@SerializedName("type")
@Expose
private String type;
@SerializedName("displayType")
@Expose
private String displayType;
@SerializedName("imageId")
@Expose
private String imageId;
protected Devices(Parcel in) {
type = in.readString();
displayType = in.readString();
imageId = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeValue(type);
parcel.writeValue(displayType);
parcel.writeValue(imageId);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDisplayType() {
return displayType;
}
public void setDisplayType(String displayType) {
this.displayType = displayType;
}
public String getImageId() {
return imageId;
}
public void setImageId(String imageId) {
this.imageId = imageId;
}
the thing is I wanted to list all the displayType dynamically based on json into spinner in my activity.
Upvotes: 0
Views: 372
Reputation: 2367
Use Custom adapter to pass the list of data you want to show in spinner something like this, depending on the UI you want to show.
public class CustomAdapter extends ArrayAdapter<String>{
private Activity activity;
private ArrayList data;
public Resources res;
SpinnerModel tempValues=null;
LayoutInflater inflater;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(
CustomSpinner activitySpinner,
int textViewResourceId,
ArrayList objects,
Resources resLocal
)
{
super(activitySpinner, textViewResourceId, objects);
/********** Take passed values **********/
activity = activitySpinner;
data = objects;
res = resLocal;
/*********** Layout inflator to call external xml layout () **********************/
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
// This funtion called for each row ( Called data.size() times )
public View getCustomView(int position, View convertView, ViewGroup parent) {
/********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
View row = inflater.inflate(R.layout.spinner_rows, parent, false);
/***** Get each Model object from Arraylist ********/
tempValues = null;
tempValues = (SpinnerModel) data.get(position);
TextView label = (TextView)row.findViewById(R.id.company);
TextView sub = (TextView)row.findViewById(R.id.sub);
ImageView companyLogo = (ImageView)row.findViewById(R.id.image);
if(position==0){
// Default selected Spinner item
label.setText("Please select company");
sub.setText("");
}
else
{
// Set values for spinner each row
label.setText(tempValues.getCompanyName());
sub.setText(tempValues.getUrl());
companyLogo.setImageResource(res.getIdentifier
("com.androidexample.customspinner:drawable/"
+ tempValues.getImage(),null,null));
}
return row;
}
}
Upvotes: 1
Reputation: 358
You have to create a String Arraylist and put it in your spinner adapter.
ArrayList<String> deviceTypeList = new ArrayList<String>();
for (device : Devices ){
deviceTypeList.add (device.getDisplayType())
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, deviceTypeList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourSpinnerName.setAdapter (adapter)
Upvotes: 0