我无法在Jsonette组件列表中找到Spinner。是否有任何替代品允许用户从列表中选择一个项目?或者我应该按照Jaonette文档中的说明开发组件?
答案 0 :(得分:4)
我找不到一个微调器,所以写了我自己的(非常原始的)。以下是代码:
public class JasonSpinnerComponent {
public static View build(View view, final JSONObject component, final JSONObject parent, final Context context) {
if(view == null) {
return new Spinner(context);
} else {
try {
String data = component.getString("data");
final String[] spinnerData = data.split("\\|");
view = JasonComponent.build(view, component, parent, context);
JSONObject style = JasonHelper.style(component, context);
String type = component.getString("type");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
(context, android.R.layout.simple_spinner_item,
spinnerData);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
((Spinner)view).setAdapter(spinnerArrayAdapter);
((Spinner)view).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
try {
((JasonViewActivity) context).model.var.put(component.getString("name"), spinnerData[position]);
} catch (JSONException e) {
Log.e("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
((Spinner)view).requestLayout();
return view;
} catch (Exception e){
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
return new View(context);
}
}
}
}
这适用于像这样的jasonette:
{
"type": "spinner",
"name": "gender",
"data": "Male|Female"
}
让我再说一遍,这是very
原始的,将来会经历很多变化。但对于寻找旋转器的人来说,这可能是一个很好的起点。如果能帮助你,我会很高兴。