我有在微調放提示自定義適配器問題。 我做了擴展ArrayAdapter類AdapterSpinnerhint:錯誤「java.lang.String中不能轉換爲java.lang.Integer中的」自定義適配器
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.
}
而且在我的活動我創建了一個功能,用於創建微調
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());
}
我想創建一個通用適配器,包括所有微調暗示。 getView看 - java.lang.String中不能轉換爲java.lang.Integer中
'(Integer)getItem(getCount())',爲什麼? – Blackbelt
嘗試將String.valueOf(的getItem(getCount將()) – damian
@damian但爲什麼?的getItem已經重新調整字符串 – Blackbelt