Android保存按钮未保存到数据库中

时间:2015-05-09 05:38:33

标签: android database sqlite

我的应用中有一个“保存”按钮,它似乎没有保存应有的功能。

这是按钮调用的功能:

public void newsave (View rootView) {
   final Dbhandler dbHandler = new Dbhandler(this.getActivity());

   final String distance = (result2.getText().toString());


    AlertDialog.Builder builder = new  AlertDialog.Builder(this.getActivity());
    builder.setTitle("Description");
    final AlertDialog dialog =builder.create();



                        // Set up the input
                        final EditText input = new EditText(this.getActivity());
                        input.setOnFocusChangeListener(new 
View.OnFocusChangeListener() {
                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                            if (hasFocus) {
                                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                            }
                        }
                    });
                        // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
                        input.setInputType(InputType.TYPE_CLASS_TEXT);
                        builder.setView(input);

                        // Set up the buttons
                        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                m_Text = input.getText().toString();

                                String ddesc = m_Text;
                                //saveDistance distances = new saveDistance(ddesc,distance);
                                try {
                                    dbHandler.addDistances(1,ddesc, distance);
                                    Toast.makeText(getActivity(), "Save Successful", Toast.LENGTH_LONG).show();
                                } catch (Exception e) {
                                    Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
                                }

                            }
                        });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });

                        builder.show();

这是我的数据库处理程序类

package com.spydotechcorps.hwfar.database;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.spydotechcorps.hwfar.provider.MyContentProvider;

/**
 * Created by INGENIO on 3/8/2015.
 */




public class Dbhandler extends SQLiteOpenHelper {


    private ContentResolver myCR;

    private static  int DATABASE_VERSION = 2;
    private static String DATABASE_NAME = "distanceDB.db";
    public static  String TABLE_DISTANCES = "distances";

    public static final String COLUMN_ID ="_id";
    public static final String COLUMN_DESCRIPTION = "COLUMN_DESCRIPTION";
    public static final String COLUMN_DISTANCE = "COLUMN_DISTANCE";


    public Dbhandler(Context context
            //, String name,
                       //SQLiteDatabase.CursorFactory factory, int version
                       ) {
        super(context, DATABASE_NAME,
                //factory
                null, DATABASE_VERSION);
        myCR = context.getContentResolver();    // obtaining reference to content resolver in content provider
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_DISTANCES_TABLE = "CREATE TABLE " +
                TABLE_DISTANCES + "("
                +
                COLUMN_ID +
                " _id INTEGER PRIMARY KEY," + COLUMN_DESCRIPTION
                + " TEXT," + COLUMN_DISTANCE + " REAL" + ")";
        db.execSQL(CREATE_DISTANCES_TABLE);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
                          int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_DISTANCES);
        onCreate(db);

    }


    public void addDistances(int c,String a,String b) {

        ContentValues values = new ContentValues();
        values.put(COLUMN_ID,c);
        values.put(COLUMN_DESCRIPTION, a);
        values.put(COLUMN_DISTANCE, b);

        myCR.insert(MyContentProvider.CONTENT_URI, values);
    }

    public saveDistance findDistances(String Distancesname) {
        String[] projection = {
                COLUMN_ID,
                COLUMN_DESCRIPTION, COLUMN_DISTANCE };

        String selection = "Distancesname = \"" + Distancesname + "\"";

        Cursor cursor = myCR.query(MyContentProvider.CONTENT_URI,
                projection, selection, null,
                null);

        saveDistance Distances = new saveDistance();

        if (cursor.moveToFirst()) {
            cursor.moveToFirst();
            Distances.setID(Integer.parseInt(cursor.getString(0)));
            Distances.setdesc(cursor.getString(1));
            Distances.setdistance(cursor.getString(2));
            cursor.close();
        } else {
            Distances = null;
        }
        return Distances;
    }

    public boolean deleteDistances(String Distancesname) {

        boolean result = false;

        String selection = "Distancesname = \"" + Distancesname + "\"";

        int rowsDeleted = myCR.delete(MyContentProvider.CONTENT_URI,
                selection, null);

        if (rowsDeleted > 0)
            result = true;

        return result;
    }



}

这是saveDistance类

package com.spydotechcorps.hwfar.database;

/**
 * Created by INGENIO on 3/8/2015.
 */
public class saveDistance {
    public int _id;
    public String _desc;
    public String _distance;

    public void Distances() {

    }

    /*public void Distances(int id, String desc, String distance) {
        this._id = id;
        this._desc = desc;
        this._distance = distance;
    }*/
    public void Distances(
            //int k,
        String desc, String distance) {
        //this._id=1;
        this._desc = desc;
        this._distance = distance;
    }

    public void setID(int id) {
        this._id = id;
    }

    public int getID() {
        return this._id;
    }

    public void setdesc(String desc) {
        this._desc = desc;
    }

    public String getdesc() {
        return this._desc;
    }

    public void setdistance(String distance) {
        this._distance = distance;
    }

    public String getdistance() {
        return this._distance;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以尝试更改dbHelper类的 addDistances 方法,因为您已将_id列用作整数和主键。尝试使用SQLite Inject查询而不是Content值而不传递_id列值。我认为由于主键值,数据没有插入/冲突。

您可以将_id列类型更改为“整数主键自动增量”,并且不要在插入查询中传递_id列值。 Sqlite DB将自动处理_id列值。无需从用户端提供。

我希望这会对你有所帮助。

答案 1 :(得分:0)

你在创建表格查询时犯了一个错误...检查一次然后给出反馈......

 @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_DISTANCES_TABLE = "CREATE TABLE " +
                TABLE_DISTANCES + "("
                +
                COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_DESCRIPTION + " TEXT," + COLUMN_DISTANCE + " INTEGER" + ")";
        db.execSQL(CREATE_DISTANCES_TABLE);


    }