插入数据后的Toast消息

时间:2013-12-29 11:35:06

标签: android database toast

嗨,我是新手Android和java.I有关于我的代码的问题。我想在数据库中插入数据后显示吐司,我想在插入和显示toast messega(错误的值)之前从数据库检查值edittext。任何人都可以告诉我该怎么办?

这是我的插页码。

package com.androidmeret.aractakip;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class YakitEkleActivity extends Activity {
    Button yeniYakitEkle;
    Button yeniYakitIptal;
    EditText alinanYakitdeger;
    EditText alinanLt;
    EditText aracKm;
    String selectedCarName;
    String fdate;
    DBController dbyakit = new DBController(this);
    Calendar date = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yakit_ekle);
        yeniYakitEkle =(Button) findViewById(R.id.yakitekle_ekle);
        yeniYakitIptal =(Button)findViewById(R.id.yakitekle_iptal);
        alinanLt = (EditText) findViewById(R.id.yakitekle_alinanyakit);
        alinanYakitdeger =(EditText) findViewById(R.id.yakitekle_ltfiyat);
        aracKm =(EditText) findViewById(R.id.yakitekle_arackm);
        selectedCarName = getIntent().getStringExtra("selected_car_name");
        fdate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

        final DatePickerDialog.OnDateSetListener d=new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                date.set(Calendar.YEAR, year);
                date.set(Calendar.MONTH, monthOfYear);
                date.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                dbyakit.insertYakit(selectedCarName, alinanLt.getText().toString(), alinanYakitdeger.getText().toString(), aracKm.getText().toString(), dayOfMonth, monthOfYear, year);
                Intent yakitMain = new Intent (getApplicationContext(),YakitActivity.class);
                yakitMain.putExtra("selected_car_name", selectedCarName);
                startActivity(yakitMain);
            }
        };

        yeniYakitEkle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //if(checkKm()=true){
                    new DatePickerDialog(YakitEkleActivity.this, d, date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DAY_OF_MONTH)).show();
                //}else{
                //    Toast.makeText(getApplicationContext(),"Girilen KM hatalı",Toast.LENGTH_LONG).show();
                //}
            }
        });

        yeniYakitIptal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent backYakit = new Intent(getApplicationContext(), YakitActivity.class);
                backYakit.putExtra("selected_car_name", selectedCarName);
                startActivity(backYakit);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.yakit_ekle, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_yakit_ekle, container, false);
            return rootView;
        }
    }

}

这是我的dbhelper ......

package com.androidmeret.aractakip;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class DBController extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="aractakip.db";
    private static String LOGCAT = null;
    private static final int SURUM = 1;
    //CARS tablosu...
    private static final String TABLO_ADI = "CARS";
    private static final String SATIR_ID = "cid";
    private static final String SATIR_ADI = "cname";
    private static final String SATIR_MODEL ="cmodel";
    private static final String SATIR_MARKA ="cbrand";
    private static final String SATIR_YIL ="cyear";
    private static final String SATIR_KM ="ckm";

    //FUEL tablosu...
    private static final String FUEL_TABLO_ADI = "FUEL";
    private static final String FUEL_ID = "fid";
    private static final String FUEL_SATIR_ADI = "fcarid";
    private static final String FUEL_LT = "flitre";
    private static final String FUEL_FIYAT ="ffiyat";
    private static final String FUEL_KM ="fkm";
    private static final String FUEL_DATE_D = "fdated";
    private static final String FUEL_DATE_M = "fdatem";
    private static final String FUEL_DATE_Y = "fdatey";
    Integer aracLastKm;
    Integer girilenKm;
    int lastKm;

    public DBController(Context dbcontext){
        super(dbcontext,DATABASE_NAME,null,SURUM);
        Log.d(LOGCAT, "Database yaratıldı.");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String queryTable = "CREATE TABLE " + TABLO_ADI + "(" + SATIR_ID + " INTEGER PRIMARY KEY," + SATIR_ADI +" TEXT,"+ SATIR_MODEL + " TEXT,"+ SATIR_MARKA + " TEXT,"+ SATIR_YIL + " TEXT,"+ SATIR_KM + " TEXT)";
        String queryFuel = "CREATE TABLE " + FUEL_TABLO_ADI + "("+ FUEL_ID + " INTEGER PRIMARY KEY," + FUEL_SATIR_ADI + " TEXT," + FUEL_LT + " TEXT,"+ FUEL_FIYAT + " TEXT," + FUEL_KM + " TEXT," + FUEL_DATE_D + " INTEGER, "+ FUEL_DATE_M + " INTEGER, " + FUEL_DATE_Y + " INTEGER)";
        db.execSQL(queryTable);
        db.execSQL(queryFuel);
        Log.d(LOGCAT, "Fuel Tablo yaratldı.");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String queryUpdatedb = "DROP TABLE IF EXISTS " + TABLO_ADI;
        db.execSQL(queryUpdatedb);
    }

   /* public boolean checkKm(String fcarid, String userKm){
        SQLiteDatabase cardatabase = getReadableDatabase();
        String query1 = "SELECT " + FUEL_KM + " FROM "+ FUEL_TABLO_ADI + " WHERE " + FUEL_SATIR_ADI + " = '"+ fcarid + "'"+ " ORDER BY " + FUEL_ID +" DESC LIMIT 1";
        Cursor check = cardatabase.rawQuery(query1,null);
        lastKm = Integer.parseInt(userKm);
        if (check.getCount()==0){
            aracLastKm = 0;
        }else {
            check.moveToFirst();
            aracLastKm = check.getInt(0);
        }
        if (aracLastKm == 0 || lastKm > aracLastKm){
            return true;
        }
        return false;

    }
*/
    public void insertYakit(String fcarid, String flitre,String ffiyat, String fkm, int fdated,int fdatem, int fdatey ){
        SQLiteDatabase cardatabase = this.getWritableDatabase();
        String query1 = "SELECT " + FUEL_KM + " FROM "+ FUEL_TABLO_ADI + " WHERE " + FUEL_SATIR_ADI + " = '"+ fcarid + "'"+ " ORDER BY " + FUEL_ID +" DESC LIMIT 1";
        Cursor check = cardatabase.rawQuery(query1,null);
        girilenKm = Integer.parseInt(fkm);

        if (check.getCount()==0){
            aracLastKm = 0;
           }else {
            check.moveToFirst();
            aracLastKm = check.getInt(0);
        }

        if( check.getCount() == 0 || girilenKm > aracLastKm ){
        ContentValues values = new ContentValues();
        values.put(FUEL_SATIR_ADI,fcarid);
        values.put(FUEL_LT,flitre);
        values.put(FUEL_FIYAT,ffiyat);
        values.put(FUEL_KM,fkm);
        values.put(FUEL_DATE_D,fdated);
        values.put(FUEL_DATE_M,fdatem);
        values.put(FUEL_DATE_Y, fdatey);
        cardatabase.insert(FUEL_TABLO_ADI, null, values);
        cardatabase.close();
        Log.d(LOGCAT, "Veri kaydedildi.");
        }else {
            Log.d(LOGCAT, "Girilen KM hatalı.");
        }
        cardatabase.close();
        check.close();
    }

    //Database araç kayıt...
    public void insertCar(String name,String model,String brand,String year,String km){
        SQLiteDatabase cardatabase = this.getWritableDatabase();
        String searchQuery = "SELECT " + SATIR_ID +","+ SATIR_ADI + " FROM " + TABLO_ADI + " WHERE " + SATIR_ADI +" = '" + name.toString()+"'" ;
        Cursor cursor = cardatabase.rawQuery(searchQuery, null);

        if (cursor.getCount() == 0){
            ContentValues values = new ContentValues();
            values.put(SATIR_ADI,name);
            values.put(SATIR_MODEL,model);
            values.put(SATIR_MARKA,brand);
            values.put(SATIR_YIL,year);
            values.put(SATIR_KM,km);
            cardatabase.insert(TABLO_ADI, null, values);
            cardatabase.close();
            Log.d(LOGCAT, "Veri kaydedildi.");
        }
        else{
            Log.d(LOGCAT, "Aynı isim var");
        }
        cursor.close();
        cardatabase.close();
    }

    //Araçları spinner doldurur...
    public List<String> getAllLabels(){
        SQLiteDatabase cardatabase =this.getWritableDatabase();
        List<String> labels = new ArrayList<String>();
        String selectQuery = "SELECT "+ SATIR_ADI + " FROM " + TABLO_ADI;
        Cursor cursor = cardatabase.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                labels.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }

        cursor.close();
        cardatabase.close();

        return labels;
    }

    //Spinner'da seçilen aracı siler.
    public void deleteCar(String cname){
        SQLiteDatabase cardatabase = this.getWritableDatabase();
        String deleteQuery = "DELETE FROM " + TABLO_ADI +" WHERE " + SATIR_ADI + " = '"+ cname +"'";
        cardatabase.execSQL(deleteQuery);
        Log.d(LOGCAT, "Araç silindi");
        cardatabase.close();
    }


}

1 个答案:

答案 0 :(得分:1)

更改insertCar方法以返回布尔结果。像那样:

   public boolean insertCar(String name,String model,String brand,String year,String km){
    SQLiteDatabase cardatabase = this.getWritableDatabase();
    String searchQuery = "SELECT " + SATIR_ID +","+ SATIR_ADI + " FROM " + TABLO_ADI + " WHERE " + SATIR_ADI +" = '" + name.toString()+"'" ;
    Cursor cursor = cardatabase.rawQuery(searchQuery, null);
    boolean c;

    if (cursor.getCount() == 0){
        ContentValues values = new ContentValues();
        values.put(SATIR_ADI,name);
        values.put(SATIR_MODEL,model);
        values.put(SATIR_MARKA,brand);
        values.put(SATIR_YIL,year);
        values.put(SATIR_KM,km);
        cardatabase.insert(TABLO_ADI, null, values);
        cardatabase.close();
        Log.d(LOGCAT, "Veri kaydedildi.");
        c=true;

    }
    else{
        Log.d(LOGCAT, "Aynı isim var");
        c=false;
    }
    cursor.close();
    cardatabase.close();
    return c;
}`

和你的

  

dbyakit.insertYakit

要insertCar,请尝试:

    if (dbyakit.insertYakit(selectedCarName, alinanLt.getText().toString(), alinanYakitdeger.getText().toString(), aracKm.getText().toString(), dayOfMonth, monthOfYear, year)) 
      // Case return true
      Toast.makeText(rootView.getContext(), "Yout text here", Toast.LENGTH_LONG).show();
    else
      // Case return false
      Toast.makeText(rootView.getContext(), "Yout text here", Toast.LENGTH_LONG).show();

干杯!