如何将最后记录的值设置为微调器下拉列表?

时间:2017-12-05 11:23:19

标签: android sqlite android-sqlite

String Food;
String[] FoodArray;
Spinner spinFood;
spinFood= (Spinner) findViewById(R.id.spin_food);
FoodArray=getResources().getStringArray(R.array.carray);
spinFood.setAdapter(new 
CustomSpinnerAdapter(ChildActivity.this,R.layout.activity_child,FoodArray));

spinFood.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
{
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int 
        position, long id) {

            if(position==1){
                Food=getString(R.string.textYes);
            }
            else if (position==2){
                Food=getString(R.string.textNo);
            }
            else {
               Food="0";
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

我尝试了很多,但没有得到答案,  请帮帮我。

默认情况下,微调器应设置为来自数据库的最后一个记录值。

2 个答案:

答案 0 :(得分:3)

 String compareValue;
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.array, android.R.layout.simple_spinner_item);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinFood.setAdapter(new CustomSpinnerAdapter(
                        getApplicationContext().this, R.layout.yourlayout,
                        compareValue));
                if (!compareValue.equals(null)) {
                    int spinnerPosition = adapter.getPosition(compareValue);
                    spinFood.setSelection(spinnerPosition);
                }

取你最后记录的值,即来自sqlite数据库的compareValue,并将该值设置为ArrayAdapter并检查null条件。

答案 1 :(得分:1)

您可以使用spinner.setSelection()方法记录上次交易