2016-02-13 161 views
1

我已经查看了将spinner保存到数据库中的所有问题,但似乎没有任何工作。将Spinner选择保存到SQLite数据库中

我已经开始在ItemSelectedListener加入工作,但它抛出回一个错误,

这里是我的代码

OnItemSelectedListener在这其中我对表的其余部分刀片是

public class OnClickListenerCreateMenuItem implements View.OnClickListener { 
@Override 
public void onClick(View view) { 


    final Context context = view.getContext(); 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View formElementsView = inflater.inflate(R.layout.menuitem_input_form, null, false); 

    final EditText editTextItemName = (EditText) formElementsView.findViewById(R.id.editTextItemName); 
    final EditText editTextItemDesc = (EditText) formElementsView.findViewById(R.id.editTextItemDesc); 
    final EditText editTextItemPrice = (EditText) formElementsView.findViewById(R.id.editTextItemPrice); 
    final EditText editTextItemCost = (EditText) formElementsView.findViewById(R.id.editTextItemCost); 
    final Spinner itemCategory = (Spinner) formElementsView.findViewById(R.id.spinnerCategory); 

    itemCategory.setAdapter(
      ((SettingsMenuItem) context).loadSpinnerData() 
    ); 

    itemCategory.setOnItemSelectedListener(
      ((SettingsMenuItem) context).loadSpinnerData() 
    ); 


    new AlertDialog.Builder(context) 
      .setView(formElementsView) 
      .setTitle("Create Item") 
      .setPositiveButton("Add", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 


          String itemName = editTextItemName.getText().toString(); 
          String itemDescription = editTextItemDesc.getText().toString(); 
          String itemPrice = editTextItemPrice.getText().toString(); 
          String itemCost = editTextItemCost.getText().toString(); 
          String valueOfSelectedCat= itemCategory.getSelectedItem().toString(); 


          ObjectMenuItem objectMenuItem = new ObjectMenuItem(); 
          objectMenuItem.name = itemName; 
          objectMenuItem.description = itemDescription; 
          objectMenuItem.price = Float.parseFloat(itemPrice); 
          objectMenuItem.cost = Float.parseFloat(itemCost); 
          objectMenuItem.cat = valueOfSelectedCat; 

          boolean createSuccessful = new TableControllerMenuItem(context).create(objectMenuItem); 


          if(createSuccessful){ 
           Toast.makeText(context, "Item was saved.", Toast.LENGTH_SHORT).show(); 
          }else{ 
           Toast.makeText(context, "Unable to save item.", Toast.LENGTH_SHORT).show(); 
          } 
          ((SettingsMenuItem) context).readRecords(); 
          ((SettingsMenuItem) context).countRecords(); 



          dialog.cancel(); 

         } 

        }).show(); 




} 

和我的实际微调器在这里设置并填充在这里

public class SettingsMenuItem extends AppCompatActivity { 

Spinner spinner; 
Context context; 
String id; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings_menu_item); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    countRecords(); 
    readRecords(); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    Button buttonCreateLocation = (Button) findViewById(R.id.btnAdd); 
    buttonCreateLocation.setOnClickListener(new OnClickListenerCreateMenuItem()); 
} 

public void countRecords() { 

    int recordCount = new TableControllerMenuItem(this).count(); 
    TextView textViewRecordCount = (TextView) findViewById(R.id.textViewRecordCount); 
    textViewRecordCount.setText(recordCount + " records found."); 

} 

public void readRecords() { 

    LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords); 
    linearLayoutRecords.removeAllViews(); 

    List<ObjectMenuItem> menuitem = new TableControllerMenuItem(this).read(); 

    if (menuitem.size() > 0) { 

     for (ObjectMenuItem obj : menuitem) { 

      int id = obj.id; 
      String menuitemName = obj.name; 
      String menuitemDescription = obj.description; 
      Float menuitemPrice = obj.price; 
      Float menuitemCost = obj.cost; 
      String itemCategory = obj.cat; 

      String textViewContents = menuitemName; 

      TextView textViewMenuItem = new TextView(this); 
      textViewMenuItem.setPadding(0, 10, 0, 10); 
      textViewMenuItem.setText(textViewContents); 
      textViewMenuItem.setTag(Integer.toString(id)); 
      textViewMenuItem.setTextSize(20.0f); 

      textViewMenuItem.setOnLongClickListener(new OnLongClickListenerMenuItem()); 


      linearLayoutRecords.addView(textViewMenuItem); 
     } 

    } else { 

     TextView locationItem = new TextView(this); 
     locationItem.setPadding(8, 8, 8, 8); 
     locationItem.setText("No records yet."); 

     linearLayoutRecords.addView(locationItem); 
    } 

} 

public ArrayAdapter loadSpinnerData() { 

    Log.d("CON", "setting spinner data"); 

    Spinner s = (Spinner) findViewById(R.id.spinnerCategory); 
    DatabaseHandler h = new DatabaseHandler(getApplicationContext()); 

    List<String> names = h.getAllNames(); 

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, names); 

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 




    return dataAdapter; 


} 





    } 

这里是我的崩溃日志

---------崩溃 8月2日至14日的开始:29:07.627 14332-14332/com.jenovaprojects.restaurant E/AndroidRuntime:致命异常:主要 过程:com.jenovaprojects.restaurant,PID:14332 java.lang.ClassCastException:android.widget.ArrayAdapter不能在com.jenovaprojects.restaurant.OnClickListenerCreate.OnClickListenerCreateMenuItem.onClick被强制转换为android.widget.AdapterView $ OnItemSelectedListener (OnClickListenerCreateMenuItem .java:41) at android.view.View.performClick(View.java:5204) at android.view.View $ PerformClick.run(View.java:21156) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5466) at java.lang.reflect.Method.invoke(Native Method) at com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

回答

1

使用来自Android指南此代码,您应该能够实现OnItemSelectedListener

public class SpinnerActivity extends Activity implements OnItemSelectedListener { 
... 

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
     // An item was selected. You can retrieve the selected item using 
     parent.getItemAtPosition(pos) 
    } 
} 

我不知道为什么它不会工作,但很难说没有更多的代码。如果这没有帮助,请发布您用于保存选择的代码,以便我们可以尝试弄清楚发生了什么。

1

请把你的LogCat看到这些错误,如果可能的话,显示你想要插入选定数据的数据库查询和表结构。

简单的程序是把onItemSelected的一个听众对你的情况微调把这个

itemCategory.setOnItemSelectedListener(replce your context here); 

然后做要紧实施微调的界面

工具OnItemSelectedListener

时您从微调器中选择任何项目此方法onItemSelected将被调用。它会将您点击的物品的位置返回给您,并且您可以轻松找出哪个物品被点击。

adapter.getItemAtPosition(pos).toString() 

通过这个,你会得到该项目的文本。

第二部分 - 插入数据的SQLite数据库

使数据库对象和初始化然后调用适当的方法来将您选择的选择数据。