2016-07-19 57 views
-2

我有在微調放提示自定義適配器問題。 我做了擴展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中

+2

'(Integer)getItem(getCount())',爲什麼? – Blackbelt

+1

嘗試將String.valueOf(的getItem(getCount將()) – damian

+0

@damian但爲什麼?的getItem已經重新調整字符串 – Blackbelt

回答

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((字符串)的getItem(getCount將()) 因爲我不能」編譯應用程序沒有。

0

在你的函數:

java.lang.ClassCastException: 但我在這行混得AdapterSpinnerHint這個錯誤這行 - .setHint((Integer)getItem(getCount()));

.setHint(串)需要字符串作爲參數,以及要通過解析字符串作爲整數傳遞一個整數。

做這樣

.setHint(的getItem(getCount將));

當你調用ArrayAdapter的構造超級傳遞字符串列表作爲參數,基本上你告訴一個ArrayAdapter,他們鍵入的對象,將在該列表將字符串。所以,當你這樣做的getItem(),將返回一個字符串

+0

謝謝你, 我在Android的新節目,當然我做錯了什麼,但如果沒有隔鑄造無法填補因的getItem(getCount將()))給我一個對象。 但顯然我錯了別的地方。 – LorenzoBerti

+0

你認爲它會返回什麼樣的對象? –

+0

查看更新的答案! –

相關問題