I am having trouble creating a Custom Adapter that will allow me to use several different views in a ListView depending on the results from JSON data.
The JSON has 5 different types of data fields and each one needs to have a separate layout. For example, the "text" field needs a TextView and then a EditText element and the "bool" field needs a TextView and a Switch element.
The JSON data could include any variation of the 5 data fields so I can't create a static layout.
I have created a separate xml file for each type of data field.
Below is the current state of my Custom Adapter with some JSON hard coded into it.
Thanks in advance!
public class JobAdapter extends ArrayAdapter<String> {
private static final String JSON_STRING = "{\"title\":\"Unity Gain Amp install\",\"submit\":\"http://dci-services.decisiveinc.net/ampinstall/\n" +
"\n" +
"Submit\",\"sections\":[{\"title\":\"Unity Gain Amp \n" +
"\n" +
"Install\",\"fields\":[{\"name\":\"TechNumber\",\"type\":\"number\",\"label\":\"Tech \n" +
"\n" +
"Number\",\"required\":true,\"stored\":true},{\"name\":\"AmpSerial\",\"type\":\"string\",\"label\":\"Amp Serial \n" +
"\n" +
"#\",\"required\":true,\"stored\":false},{\"name\":\"Account\",\"type\":\"number\",\"label\":\"Account \n" +
"\n" +
"#\",\"required\":true,\"stored\":false}]},{\"title\":\"Reason For Amp \n" +
"\n" +
"Install\",\"fields\":[{\"name\":\"Reason\",\"type\":\"select\",\"label\":\"Reason\",\"required\":true,\"stored\":f\n" +
"\n" +
"alse,\"options\":[{\"key\":\"1\",\"label\":\"Low receive power\"},{\"key\":\"2\",\"label\":\"High \n" +
"\n" +
"Transmit \"},{\"key\":\"3\",\"label\":\"Both\"},{\"key\":\"4\",\"label\":\"Replacement\"}\n" +
"\n" +
"]},{\"name\":\"OldAmpSN\",\"type\":\"string\",\"label\":\"If Replacement: Old Amp \n" +
"\n" +
"SN\",\"required\":false,\"stored\":false}]}]}";
int resource;
String response;
Context context;
//Initialize Adapter
public JobAdapter(Context context, int resource, List<String> items) {
super(context, resource, items);
this.resource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout jobView;
//Get the current job object
String job = getItem(position);
//Inflate the view
if(convertView==null) {
jobView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, jobView, true);
}
else
{
jobView = (LinearLayout) convertView;
}
TextView job_title = (TextView)jobView.findViewById(R.id.job_title);
TextView text_title = (TextView)jobView.findViewById(R.id.job_text_title);
EditText text_input = (EditText)jobView.findViewById(R.id.job_text_input);
TextView select_title = (TextView)jobView.findViewById(R.id.job_select_title);
Spinner select_array = (Spinner) jobView.findViewById(R.id.job_select_spinner);
TextView location_title = (TextView)jobView.findViewById(R.id.job_loc_title);
TextView location_text = (TextView) jobView.findViewById(R.id.job_loc_text);
TextView image_title = (TextView)jobView.findViewById(R.id.job_image_title);
ImageView image_view = (ImageView)jobView.findViewById(R.id.job_image_thumb);
TextView bool_title = (TextView)jobView.findViewById(R.id.job_bool_title);
Switch bool_switch = (Switch)jobView.findViewById(R.id.job_bool_switch);
try {
JSONObject sections =
(new JSONObject(JSON_STRING)).getJSONObject("sections");
String jobtitle = sections.getString("title");
job_title.setText(jobtitle);
JSONObject fields =
(new JSONObject(JSON_STRING)).getJSONObject("sections").getJSONObject("fields");
JSONArray select_options =
(new JSONObject(JSON_STRING)).getJSONObject("sections")
.getJSONObject("fields").getJSONArray("options");
if (fields.getString("type").equals("number")) {
String labelnumber = fields.getString("label");
text_title.setText(labelnumber);
}
if (fields.getString("type").equals("string")) {
String labelstring = fields.getString("label");
text_title.setText(labelstring);
}
if (fields.getString("type").equals("textarea")) {
String labeltextarea = fields.getString("label");
text_title.setText(labeltextarea);
}
if (fields.getString("type").equals("bool")) {
String labelbool = fields.getString("label");
bool_title.setText(labelbool);
}
if (fields.getString("type").equals("select")) {
String labelselect = fields.getString("label");
select_title.setText(labelselect);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("Select a Reason");
for (int i = 0; i < select_options.length(); ++i)
{
adapter.add(select_options.getJSONObject(i).getString("label"));
}
select_array.setAdapter(adapter);
select_array.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View itemSelected, int itemkey, long l) {
SharedPreferences.Editor editor = jobData.edit();
editor.putInt(reason_data, itemkey);
editor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
if (fields.getString("type").equals("image")) {
String labelimage = fields.getString("label");
image_title.setText(labelimage);
}
if (fields.getString("type").equals("location")) {
String labellocation = fields.getString("label");
location_title.setText(labellocation);
}
} catch (JSONException e) {
e.printStackTrace();
}
return jobView;
}
}