我有一个自定义适配器的问题,用于在微调器中放置提示。 我已经创建了一个扩展ArrayAdapter的类AdapterSpinnerhint:
public class AdapterSpinnerHint extends ArrayAdapter {
int labelHint;
int textViewId;
int layout;
ArrayList<String> mItems;
Context context;
public AdapterSpinnerHint(Context context, int spinner_layout, int field, ArrayList<String> list, int label) {
super(context, spinner_layout, list);
textViewId = field;
labelHint = label;
layout = spinner_layout;
mItems=list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(layout, null);
if (position == getCount()) {
((TextView)v.findViewById(textViewId)).setText(labelHint);
((TextView)v.findViewById(textViewId)).setHint((Integer) getItem(getCount())); //"Hint to be displayed"
}
return v;
}
@Override
public int getCount() {
return mItems.size()-1; // you dont display last item. It is used as hint.
}
在我的活动中,我为Create spinner
创建了一个函数private void createSpinnerCustomer(JSONArray customers) {
Spinner spinner = (Spinner) findViewById(R.id.customers_spinner);
ArrayList<String> customersList=new ArrayList<String>();
for(int i=0;i<customers.length();i++){
try {
customersList.add(customers.getJSONObject(i).getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
customersList.add(String.valueOf(R.string.customer_label));
assert spinner != null;
AdapterSpinnerHint adapter=new AdapterSpinnerHint(
getApplicationContext(), R.layout.spinner_layout, R.id.txt, customersList, R.string.customer_label);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount());
}
我会为包含提示的所有微调器创建一个通用适配器。 但我在此行中的AdapterSpinnerHint上收到此错误:
java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Integer
答案 0 :(得分:0)
在你的函数中 - getView 看到这一行 - .setHint((Integer)getItem(getCount()));
.setHint(string)需要字符串作为参数,并且您通过将字符串解析为Integer来传递Integer。
喜欢这个
<强> .setHint(的getItem(getCount将)); 强>
当你在ArrayAdapter的构造函数中调用super并将字符串列表作为参数传递时,基本上你告诉ArrayAdapter它们在列表中的对象类型将是String。所以当你执行getItem()时,它将返回一个String
答案 1 :(得分:0)
我改变了做法: 在我的活动中:
private void createSpinnerCustomer(JSONArray customers) {
Spinner spinner = (Spinner) findViewById(R.id.customers_spinner);
AdapterSpinnerHint adapter2 = new AdapterSpinnerHint(ActivityActivity.this, android.R.layout.simple_spinner_dropdown_item, "Customers", customers, "name");
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assert spinner != null;
spinner.setAdapter(adapter2);
spinner.setSelection(adapter2.getCount());
}
我的适配器:
public class AdapterSpinnerHint extends ArrayAdapter {
String labelHint;
JSONArray mItems;
String property;
public AdapterSpinnerHint(Context context, int spinner_layout, String label, JSONArray list, String getValue) {
super(context, spinner_layout);
labelHint = label;
mItems = list;
property = getValue;
addValue(mItems);
}
private void addValue(JSONArray customers) {
for(int i=0;i<customers.length();i++){
try {
this.add(customers.getJSONObject(i).getString(property));
} catch (JSONException e) {
e.printStackTrace();
}
}
this.add(labelHint);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView)v.findViewById(android.R.id.text1)).setText("");
((TextView)v.findViewById(android.R.id.text1)).setHint((String) getItem(getCount())); //"Hint to be displayed"
}
return v;
}
@Override
public int getCount() {
return super.getCount()-1; // you dont display last item. It is used as hint.
}
}
它可以工作,但是我已经将强制转换放在setHint上((String)getItem(getCount()) 因为我不能&#39;编译app没有。