2016-12-11 5 views
1

Ich bin ein Anfänger Android-Programmierer. Ich habe ein Problem und kann es nicht lösen. Ich möchte die Brieftasche der Datenbank hinzufügen, aber die Anwendung wirft einen Fehler auf. Es scheint, dass es den Wert des EditText nimmt. Was ist los mit dir?Android SQLite Datenbank

Ich habe Fehler:

Error inserting currency=null surname=null start_acc=0.0 name=null 
        android.database.sqlite.SQLiteConstraintException: NOT NULL 

XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_dodawanie_portfela" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.xxx.xxx.xxx"> 

    <TextView 
     android:text="xxx" 
     android:textSize="20dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="29dp" 
     android:layout_marginStart="29dp" 
     android:layout_marginTop="75dp" 
     android:id="@+id/textView3" /> 

    <TextView 
     android:text="xxx" 
     android:textSize="20dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView3" 
     android:layout_alignLeft="@+id/textView3" 
     android:layout_alignStart="@+id/textView3" 
     android:layout_marginTop="20dp" 
     android:id="@+id/textView4" /> 

    <TextView 
     android:text="xxx" 
     android:textSize="20dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView4" 
     android:layout_alignLeft="@+id/textView4" 
     android:layout_alignStart="@+id/textView4" 
     android:layout_marginTop="20dp" 
     android:id="@+id/textView5" /> 

    <TextView 
     android:text="xxx" 
     android:textSize="20dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView5" 
     android:layout_alignLeft="@+id/textView5" 
     android:layout_alignStart="@+id/textView5" 
     android:layout_marginTop="20dp" 
     android:id="@+id/textView6" /> 

    <Spinner 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView6" 
     android:layout_toRightOf="@+id/textView3" 
     android:layout_toEndOf="@+id/textView3" 
     android:layout_marginLeft="26dp" 
     android:layout_marginStart="26dp" 
     android:id="@+id/curenncy" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:layout_above="@+id/textView4" 
     android:layout_alignLeft="@+id/curenncy" 
     android:layout_alignStart="@+id/curenncy" 
     android:id="@+id/name" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:layout_above="@+id/textView5" 
     android:layout_alignLeft="@+id/name" 
     android:layout_alignStart="@+id/name" 
     android:id="@+id/surname" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:layout_above="@+id/textView6" 
     android:layout_alignLeft="@+id/curenncy" 
     android:layout_alignStart="@+id/curenncy" 
     android:id="@+id/cash" /> 

    <Button 
     android:text="ADD" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/addwallet" 
     android:width="500dp" 
     android:background="@color/common_action_bar_splitter" 
     android:elevation="0dp" 
     android:layout_marginTop="78dp" 
     android:layout_below="@+id/textView6" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

Methode

public void AddWallet(String name, String surname, int start_acc, int curenncy){ 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(Wallets.COLUMNS.name, wallet.getName()); 
    contentValues.put(Wallets.COLUMNS.surname, wallet.getSurname()); 
    contentValues.put(Wallets.COLUMNS.start_acc, wallet.getStart_acc()); 
    contentValues.put(Wallets.COLUMNS.curenncy, wallet.getCurrency()); 
    DbHelper.getWritableDatabase().insert(Wallets.NAME_TABLE,null,contentValues); 
} 

Klasse

public class Add extends AppCompatActivity { 
EditText name; 
EditText surname; 
EditText start_acc; 
Button add; 
Spinner curenncy; 

int id_value; 
DatabaseDEO db = new DatabaseDEO(this); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_add); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    name = (EditText) findViewById(R.id.name); 
    surname = (EditText) findViewById(R.id.surname); 
    start_acc = (EditText) findViewById(R.id.cash); 
    add = (Button) findViewById(R.id.addwallet); 
    curenncy = (Spinner) findViewById(R.id.curenncy); 


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


      id_value = position; 

     } 

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

     } 
    }); 


    final ArrayList<String> list= db.getToSpinner(); 
    ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,R.layout.currency_spinner, R.id.text,list); 
    curenncy.setAdapter(adapter); 



    add.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      String pname = name.getText().toString(); 
      String psurname = surname.getText().toString(); 
      int pstart_acc = Integer.parseInt(start_acc.getText().toString()); 
      int pid_value = id_value; 

      db.AddWallet(pname,psurname,pstart_acc,pid_value); 

      Toast.makeText(getApplicationContext(), "Added", Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 
+0

Eine Ihrer Spalten akzeptiert keine Nullwerte, aber Sie erhalten einen Nullwert aus dem Bearbeitungstext. vergewissere dich, dass du etwas in edittext eingibst. Sie sollten auch vor dem Einfügen der Daten auf Null prüfen. –

+0

Ja, eine Spalte hat kein Nullattribut, aber ich habe Text in Edittext geschrieben und wähle Optionen in Spinner. – Adrien

Antwort

0

Ändern Sie den obigen Code und es wird funktionieren. Ich nehme an, Sie haben Werte aus einer Modellklassen-Objektmappe genommen, aber Sie haben sie nicht gesetzt.

public void AddWallet(String name, String surname, int start_acc, int curenncy){ 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(Wallets.COLUMNS.name, name); 
    contentValues.put(Wallets.COLUMNS.surname, surname); 
    contentValues.put(Wallets.COLUMNS.start_acc, start_acc); 
    contentValues.put(Wallets.COLUMNS.curenncy,curenncy); 
    DbHelper.getWritableDatabase().insert(Wallets.NAME_TABLE,null,contentValues); 
} 
+0

Danke! Arbeit! Aber ich kann keine ID curenncy vom Spinner bekommen. Vielleicht wissen Sie, wie es geht? Methode curenncy.setOnItemSelectedListener muss aufgerufen werden? – Adrien

+0

@Adrien Warum speichern Sie die Spinnerposition in der Datenbank? Sie sollten den ausgewählten Elementwert darin speichern. Sie können curency.getSelectedItem(). ToString(); Und wenn Sie einen Integer-Wert haben, können Sie ihn nach int parsen. –

+0

In der Datenbank habe ich Tabelle Curency, wo ist id_curency und code_currency Spalten. Spinner-Wert stammt aus der Datenbank (Daten aus Spalte code_currency). Ich möchte id_currency bekommen, welchen Benutzer wählen. – Adrien