android Spinner set text在setText中给出错误

时间:2015-06-30 08:26:26

标签: android android-edittext spinner

我正在处理EditTextSpinner。当我为EditText设置文字时,它很好,但是当我尝试为Spinner设置文字时,它会给出错误。

这是设置文字EditText

的代码
New_Name.setText(NewName);

此处New_Name为EditText

这是设置文字Spinner

的代码
New_Quantity.setText(NewQuantity);

此处New_Quantity为Spinner

这是完整的代码。

    public class UpdateFoodActivity extends Activity {
    EditText Name_Search,New_Name,New_Calorie,New_Fat,New_Protein,New_Sugar,New_carbohydrates;
    Spinner New_Quantity;
    TextView title_text;
    String SearchName,NewName,NewQuantity,NewCalorie,NewFat,NewProtein,NewSugar,Newcarbohydrates;
    FoodDbHelper foodDbHelper;
    SQLiteDatabase sqLiteDatabase;
    Button UpdateButton;
    ArrayAdapter<CharSequence> adapter;
    private Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.update_food_activity);

        Name_Search = (EditText)findViewById(R.id.name_search);
        New_Name = (EditText)findViewById(R.id.new_name);
        New_Quantity = (Spinner)findViewById(R.id.new_quantity);
        adapter = ArrayAdapter.createFromResource(this, R.array.quant, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        New_Quantity.setAdapter(adapter);
        New_Quantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

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

            }
        });
        New_Calorie = (EditText)findViewById(R.id.new_calorie);
        New_Fat = (EditText)findViewById(R.id.new_fat);
        New_Protein = (EditText)findViewById(R.id.new_protein);
        New_Sugar = (EditText)findViewById(R.id.new_sugar);
        New_carbohydrates = (EditText)findViewById(R.id.new_vitamins);
        UpdateButton = (Button)findViewById(R.id.update_button);
        title_text = (TextView)findViewById(R.id.title_text);
        New_Name.setVisibility(View.GONE);
        New_Quantity.setVisibility(View.GONE);
        New_Calorie.setVisibility(View.GONE);
        New_Fat.setVisibility(View.GONE);
        New_Protein.setVisibility(View.GONE);
        New_Sugar.setVisibility(View.GONE);
        New_carbohydrates.setVisibility(View.GONE);
        UpdateButton.setVisibility(View.GONE);
        title_text.setVisibility(View.GONE);

    }


    public void searchFood(View view){
        SearchName = Name_Search.getText().toString();
        foodDbHelper = new FoodDbHelper(getApplicationContext());
        sqLiteDatabase = foodDbHelper.getReadableDatabase();
        Cursor cursor = foodDbHelper.getFood(SearchName,sqLiteDatabase);
        if (cursor.moveToFirst()) {


            NewQuantity = cursor.getString(0);
            NewCalorie = cursor.getString(1);
            NewFat = cursor.getString(2);
            NewProtein = cursor.getString(3);
            NewSugar = cursor.getString(4);
            Newcarbohydrates = cursor.getString(5);

            NewName = SearchName;
            New_Name.setText(NewName);
            New_Quantity.setText(NewQuantity);
            New_Calorie.setText(NewCalorie);
            New_Fat.setText(NewFat);
            New_Protein.setText(NewProtein);
            New_Sugar.setText(NewSugar);
            New_carbohydrates.setText(Newcarbohydrates);
            New_Name.setVisibility(View.VISIBLE);
            New_Quantity.setVisibility(View.VISIBLE);
            New_Calorie.setVisibility(View.VISIBLE);
            New_Fat.setVisibility(View.VISIBLE);
            New_Protein.setVisibility(View.VISIBLE);
            New_Sugar.setVisibility(View.VISIBLE);
            New_carbohydrates.setVisibility(View.VISIBLE);
            UpdateButton.setVisibility(View.VISIBLE);
            title_text.setVisibility(View.VISIBLE);

        }
        else
        {
            Toast.makeText(getApplicationContext(),
                    "No Dish Found " ,
                    Toast.LENGTH_SHORT).show();


        }

    }

    public void updatebutton(View view)
    {
        foodDbHelper = new FoodDbHelper(getApplicationContext());
        sqLiteDatabase = foodDbHelper.getWritableDatabase();
        String name,quantity,calorie,fat,protein,sugar,carbohydrates;
        name = New_Name.getText().toString();
        quantity = New_Quantity.getSelectedItem().toString();
        calorie = New_Calorie.getText().toString();
        fat = New_Fat.getText().toString();
        protein = New_Protein.getText().toString();
        sugar = New_Sugar.getText().toString();
        carbohydrates = New_carbohydrates.getText().toString();
        int count = foodDbHelper.updateInformation(SearchName,name,quantity,calorie,fat,protein,sugar,carbohydrates,sqLiteDatabase);
        Toast.makeText(getApplicationContext(),count+" dish updated",Toast.LENGTH_LONG).show();
        Intent intent = new Intent(this,UpdateFoodActivity.class);
        startActivity(intent);

    }

}

2 个答案:

答案 0 :(得分:1)

为了向微调器添加条目,您需要定义一个适配器,我举个例子:

$(document).ready(function(e) {
if (screen.width > 736) { 

}
else {
      var mouseX;
      var mouseY;
      mouseX = e.pageX; 
      mouseY = e.pageY;
      $(".kunden-item").on('click', function(event){
       $("#divId").show().css( {position:"absolute", top:event.pageY, left: event.pageX});
     });
     $(".kunden-item").mouseout(function(){
         $('#divId').fadeOut('middle');
     });
   }
});  

答案 1 :(得分:0)

Spinner喜欢ListView,需要一个适配器来呈现数据。简单来说,适配器是一组项目。 (如您所见ListView始终包含项目列表以及Spinner)。尽管如此,spinner的适配器通常是一个字符串列表。 (因为spinner只代表用户选择)

在您的示例中,您只需创建ArrayAdapter并再次分配给Spinner

private void setTextForSpinner(Spinner spinner) {
   String[] items = {"Stack", "Over", "Flow"};
   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items); 
   spinner.setAdapter(adapter);
}

如果将以上代码应用于您的示例,您可以再次更改为:

private void setTextForSpinner(Spinner spinner, String NewName) {
       String[] items = {NewName};
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items); 
       spinner.setAdapter(adapter);
}

希望这有帮助:)