jeudi 4 février 2016

android database dont working

i make project with database for saving some data and show the data but i have problem

ihave three java class one of them is database

package com.farzad.droid;

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

public class HotOrNot {
public static final String KEY_ROWID = "_id"; 
public static final String KEY_NAME = "person_name";
public static final String KEY_HOTNESS = "persons hotness"; 

private static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourhelper;
private final Context ourcontext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context,KEY_NAME,null,DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }


    //create database
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        KEY_NAME + " TEXT, " +
        KEY_HOTNESS + " TEXT);"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
    }
public HotOrNot(Context c){
    ourcontext = c;
}
public HotOrNot open() throws SQLException {
    ourhelper = new DbHelper(ourcontext);
    ourDatabase = ourhelper.getWritableDatabase();
    return this;
}
public void close(){
    ourhelper.close();

}
//reseive data from other activity and save in database
public long createEntry(String name, String hotness) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_HOTNESS, hotness);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}
    //send String to other Activity
    public String getdata() {
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null,null,null, null);
    String resault = "";
    int irow = c.getColumnIndex(KEY_ROWID);
    int iname = c.getColumnIndex(KEY_NAME);
    int ihotness = c.getColumnIndex(KEY_HOTNESS);
    for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
        resault = resault + c.getString(0) + " " + c.getString(iname) + c.getString(ihotness)+ "/n";

    }
    return resault;
}
}

and the other one is SQLView for showing data in database

package com.farzad.droid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SQLView extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);
    TextView tv = (TextView)findViewById(R.id.textView3);
    HotOrNot info = new HotOrNot(this);
    info.open();
    String data = info.getdata();
    info.close();
    tv.setText(data);
}


}

and the other one is the class consist of two button and two editText and one button save data was inserted in editText in database and the other one show the data.

package com.farzad.droid;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



 public class AppActivity extends Activity implements OnClickListener    {

 EditText et , et1;
 Button bt , bt1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et = (EditText)findViewById(R.id.editText1);
    et1 = (EditText)findViewById(R.id.editText2);
    bt = (Button)findViewById(R.id.button1);
    bt1 = (Button)findViewById(R.id.button2);
    bt.setOnClickListener(this);
    bt1.setOnClickListener(this);

}

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){

    case R.id.button1:
        boolean diditwork = true;
        try{
        String name = et.getText().toString();
        String hotness = et1.getText().toString();
        HotOrNot entry = new HotOrNot(AppActivity.this);
        entry.open();
        entry.createEntry(name , hotness);
        entry.close();
        break;
        }catch(Exception e){
            diditwork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("thats right");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();

        }finally{
            if(diditwork){
                Dialog d = new Dialog(this);
                d.setTitle("thats right");
                TextView tv = new TextView(this);
                tv.setText("success");
                d.setContentView(tv);
                d.show();
            }
        }
    case R.id.button2:
        Intent inll = new Intent(AppActivity.this,SQLView.class);
        startActivity(inll);
        break;

    }
}
}

Aucun commentaire:

Enregistrer un commentaire