2013-02-20 146 views
0

問題是在微調器中選擇的項目呈現白色,但我已明確設置顏色。我不知道該怎麼做。我搜索了類似的主題,但他們通過設置顏色來解決。對我來說它沒有幫助。這裏是我的代碼:Android:自定義微調器不顯示選定的項目

private View getFormView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater inflater = mA.getLayoutInflater(); 
     v = inflater.inflate(R.layout.f_new_program_form, null); 
     Spinner sp = (Spinner) v.findViewById(R.id.spinnerExercises); 
     ExerciseSpinnerAdapter spAdapter = new ExerciseSpinnerAdapter(mA); 

     LoadTask task = new LoadTask((BBBaseAdapter) spAdapter, 
       URLs.host + URLs.allExercisesUrl, mA); 
     task.execute(); 
     sp.setAdapter(spAdapter); 
    } 
    return v; 
} 

ExerciseSpinnerAdapter.java:

public class ExerciseSpinnerAdapter extends BBBaseAdapter implements 
    SpinnerAdapter { 

protected JSONArray data = new JSONArray(); 

public ExerciseSpinnerAdapter(Activity a) { 
    super(a); 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return this.data.length(); 
} 

@Override 
public JSONObject getItem(int position) { 
    try { 
     return this.data.getJSONObject(position); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public int getItemViewType(int position) { 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     v = mA.getLayoutInflater().inflate(R.layout.custom_spinner, parent, 
       false); 

    } 
    TextView t = (TextView) v.findViewById(R.id.spinnerTextZZ); 
    try { 
     t.setText(getItem(position).getString("name")); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    t.setTextColor(Color.RED); 

    return v; 
} 

@Override 
public int getViewTypeCount() { 
    // TODO Auto-generated method stub 
    return 1; 
} 

@Override 
public boolean hasStableIds() { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public boolean isEmpty() { 
    // TODO Auto-generated method stub 
    return false; 
} 

@Override 
public void registerDataSetObserver(DataSetObserver observer) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void unregisterDataSetObserver(DataSetObserver observer) { 
    // TODO Auto-generated method stub 

} 

@Override 
public View getDropDownView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     v = mA.getLayoutInflater().inflate(R.layout.custom_spinner, parent, 
       false); 

    } 
    TextView t = (TextView) v.findViewById(R.id.spinnerTextZZ); 
    try { 
     t.setText(getItem(position).getString("name")); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    t.setTextColor(Color.RED); 
    return v; 
} 

@Override 
public void updateEntries(Object data) { 
    this.data = (JSONArray) data; 
    notifyChanges(); 
} 

public void notifyChanges() { 
    notifyDataSetChanged(); 
} 

} 

custom_spinner:

< TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/spinnerTextZZ" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="0dp" 
    android:layout_margin="0dp" 
    android:text="!" 
    android:textAppearance="?android:attr/textAppearanceMedium" android:textColor = "#000000" /> 

有什麼我錯過了?

回答

0

如果你有多餘的方法,我有:

@Override 
public int getViewTypeCount() { 
// TODO Auto-generated method stub 
return 1; 
} 

@Override 
public boolean hasStableIds() { 
// TODO Auto-generated method stub 
return false; 
} 

@Override 
public boolean isEmpty() { 
// TODO Auto-generated method stub 
return false; 
} 

@Override 
public void registerDataSetObserver(DataSetObserver observer) { 
// TODO Auto-generated method stub 

} 

刪除,要麼執行。但是如果你擴展了BaseAdapter,它們已經被實現了。不知道我從哪個例子中得到了它們。