我在创建自定义适配器时遇到问题,这将允许我根据JSON数据的结果在ListView中使用多个不同的视图。
JSON有5种不同类型的数据字段,每种数据字段都需要有一个单独的布局。例如,“text”字段需要TextView,然后是EditText元素,“bool”字段需要TextView和Switch元素。
JSON数据可能包含5个数据字段的任何变体,因此我无法创建静态布局。
我为每种类型的数据字段创建了一个单独的xml文件。
以下是我的自定义适配器的当前状态,其中一些JSON硬编码到其中。
提前致谢!
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;
}
}