2014-12-05 169 views
0

enter image description here如何從自定義微調Android中

公共類MainActivity刪除選擇項目延伸活動{

String[] DayOfWeek = { "Select Item", "1", "2", "3", "4", "5", "6" }; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Spinner mySpinner = (Spinner) findViewById(R.id.spinner); 
    mySpinner.setAdapter(new MyCustomAdapter(MainActivity.this, 
      R.layout.row, DayOfWeek)); 
} 

public class MyCustomAdapter extends ArrayAdapter<String> { 

    public MyCustomAdapter(Context context, int textViewResourceId, 
      String[] objects) { 
     super(context, textViewResourceId, objects); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, 
      ViewGroup parent) { 
     // TODO Auto-generated method stub 
     return getCustomView(position, convertView, parent); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     return getCustomView(position, convertView, parent); 
    } 

    public View getCustomView(int position, View convertView, 
      ViewGroup parent) { 
     // TODO Auto-generated method stub 
     // return super.getView(position, convertView, parent); 

     LayoutInflater inflater = getLayoutInflater(); 
     View row = inflater.inflate(R.layout.row, parent, false); 
     TextView label = (TextView) row.findViewById(R.id.weekofday); 
     label.setText(DayOfWeek[position]); 

     ImageView icon = (ImageView) row.findViewById(R.id.icon); 

     if (DayOfWeek[position] == "Sunday") { 
      icon.setImageResource(R.drawable.icon); 
     } else { 
      icon.setImageResource(R.drawable.icongray); 
     } 

     return row; 
    } 
} 

}

這裏是我的代碼,我能夠在旋轉顯示的價值,但我希望顯示僅在Textview中選擇項目不在Value中,同時其顯示值也告訴我如何刪除該項目,請參閱屏幕並告訴我如何刪除該項目

+0

AFAIK這不是Android'Spinner'工作的方式:它只顯示值,而不顯示標題。所以只需使用值並刪除「選擇項目」。 – shkschneider 2014-12-05 10:40:28

+0

正如@shkschneider所述,這不是微調器的工作原理: 在微調控制器上方添加一個textView並從微調控制器中刪除「Select item」數據 – Altoyyr 2014-12-05 10:43:19

+0

使用spinner.setPrompt(「Select Item」);並從DayOfWeek數組中刪除這個「Select Item」字符串。 – 2014-12-05 10:44:17

回答

1

一個簡單的解決辦法是:
a)所述陣列修改到

String[] DayOfWeek = { "1", "2", "3", "4", "5", "6" }; 

b)中呼叫mySpinner.setPromptId(),並設置與值 「選擇信息」 的字符串reosurceId。

僅當下拉未打開或關閉時纔有「選擇項目」,請點擊此鏈接How to make an Android Spinner with initial text "Select One"

+0

我在哪裏設置mySpinner.setPromptId()? – Edge 2014-12-05 11:16:37

+0

@ 太空請檢查其 – Edge 2014-12-05 11:19:10

+0

在mySpinner初始化後的onCreate()中。 – thepace 2014-12-05 11:25:23

0
ListPopupWindow numberList; 
TextView spDays; 
ArrayList<Map<String, String>>() listTrans; 

in oncreate spDays.setonclicklistner(this);spDays.setText("Select"); 
setNumberListSpinnerView(); 
in onclick(){ 
    when spDays clicked :- numberList.show(); 
} 


void setNumberListSpinnerView() { 

    numberList= new ListPopupWindow(this); 
    numberList.setAnchorView(spDays); 

    numberList.setOnItemClickListener((new AdapterView.OnItemClickListener() { 
     @Override 
     getListItem(); 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Map map = listTrans.get(position); 
      spDays.setText(map.get("circle_name").toString()); 
      circle_name = map.get("circle_name") + ""; 
      circle_id = map.get("circle_id").toString(); 
      circleList.dismiss(); 
      Log.d("Circle id:", circle_id + ""); 
      getRetails(); 

     } 
    })); 
} 


void getListItem(){ 
    String[] numbers = {"1","2","3","4","5","6"}; 
    listTrans = new ArrayList<Map<String, String>>(); 
    LinkedHashMap<String, String> tran = new LinkedHashMap<String, String>(); 
    for (String number : numbers) { 
     tran.put("numbers", number); 
     listTrans.add(tran); 
    } 
    SimpleAdapter adapter = new SimpleAdapter(AddRetailSurvey.this, listTrans, 
      android.R.layout.simple_spinner_dropdown_item, 
      new String[]{"numbers"}, 
      new int[]{android.R.id.text1}); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    numberList.setAdapter(adapter); 
} 

更改代碼與您的要求,檢查並讓我知道如果你面臨任何問題。這項技術的最佳部分可以改變文字顏色。謝謝:)

+0

好的給我一個小例子,我可以實現這個 – Edge 2014-12-05 12:11:21

+0

好吧,等等,我會告訴你代碼。 – Biswajit 2014-12-05 12:13:42