无法输入数据库复选框

时间:2014-11-15 06:50:58

标签: java android checkbox

我想在我的database中输入复选框值。但我认为我做错了,因为database中没有输入值。我还想确保只有一个复选框一次被选中.Plz帮助我,我是android的初学者,所以请原谅我,如果这个问题太简单,不能发布。

这是我的Mainactivity.java

import android.support.v7.app.ActionBarActivity;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import java.sql.Date;
import java.util.*;
import java.text.SimpleDateFormat;
import android.widget.CheckBox;
import android.widget.CompoundButton;
public class Entertheexpenses extends ActionBarActivity  {
SQLiteDatabase db;
EditText et1,et2;
CheckBox ch,ch1;
int checked,checked1;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entertheexpenses);
    ch = (CheckBox)findViewById(R.id.checkBox1);
    ch1=(CheckBox)findViewById(R.id.checkBox2);
 et1=(EditText)findViewById(R.id.editText1);

db= openOrCreateDatabase("Mydb", MODE_PRIVATE, null);
     db.execSQL("create table if not exists food111(expenditures number(8,2),expense_date date,need number(1),want number(1))");
    ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                checked = 1;
            }else{
                checked = 0;
            }
        }
    });
    ch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                checked1 = 1;
            }else{
                checked1 = 0;
            }
        }
    });
    if(checked==1 && checked1==0)
    {

         Button but1= (Button)findViewById(R.id.button1);
       but1.setOnClickListener(new OnClickListener() 
       {
           public void onClick(View v) 
           {   
          String name=et1.getText().toString();
        double name1 = Double.parseDouble(name);

        db.execSQL("insert into food111 values("+name1+",datetime(),checked,checked1)");      
}
       });

}
    else if(checked==0 && checked1==1)
    {
         Button but1= (Button)findViewById(R.id.button1);
       but1.setOnClickListener(new OnClickListener() 
       {
           public void onClick(View v) 
           {   
          String name=et1.getText().toString();
        double name1 = Double.parseDouble(name);

        db.execSQL("insert into food111 values("+name1+",datetime(),checked,checked1)");      
}
       });
    }
 }
 }

这是我的activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:gravity="top" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="18dp"
    android:text="Enter your expenses here"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="38dp"
    android:layout_marginTop="18dp"
    android:ems="10"
    android:inputType="numberDecimal" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="22dp"
    android:text="Neccesity" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox1"
    android:layout_marginTop="24dp"
    android:text="Want" />

   <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/checkBox2"
    android:layout_marginTop="69dp"
    android:layout_toRightOf="@+id/checkBox2"
    android:text="Insert" />

   </RelativeLayout>

0 个答案:

没有答案
相关问题