0

我有一个从活动动态创建的微调器。我想从服务器中根据数据库选择一个值,所以我正在这样做

         String[] items = { "Credit Card", "Cash"};

            final Spinner TP = new Spinner(this);
            TP.setId(20);
            TP.setPadding(8,8,8,8);
            TP.setBackgroundResource(R.drawable.text_bg);
            TourExpenseListDetailRow.addView(TP);// add the column to the table row here
            LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams)TP.getLayoutParams();
            params1.setMargins(0, 0, 0, 0); //substitute parameters for left, top, right, bottom
            TP.setLayoutParams(params1);

        //Selecting value as per database for testing I am selecting Position 1 
            TP.setSelection(1);

            ArrayAdapter aa = new ArrayAdapter(this, R.layout.spinner_item, items);
            aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            TP.setAdapter(aa);

但我的问题是我的价值永远不会被选中

4

2 回答 2

3

尝试移动

TP.setSelection(1);

TP.setAdapter(aa);

还要考虑遵循 Java 命名约定。变量名应该是大小写混合的,这意味着它们以小写开头,随后的单词以大写开头,所以

Spinner TP

将会

Spinner tp

或许

Spinner tP
于 2013-05-24T01:19:10.703 回答
0

使用这样的东西

private void setUpSpinner() {
    //TODO: for the medical profile
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),R.array.demoArray,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    medicalProfile.setAdapter(adapter);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    switch (parent.getId()){
        case R.id.medicalProfile:
            String medicalStr = parent.getItemAtPosition(position).toString();
            Log.d("MedSpinner",medicalStr);
            break;

    }
}
于 2018-03-15T11:11:38.383 回答