0 Replies - 231 Views - Last Post: 17 April 2013 - 04:24 PM

#1 alphasil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-December 07

Can't see my db records

Posted 17 April 2013 - 04:24 PM

Hi

I'm doing i little program with sqlite db

Well, this is my dbadpater

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.SQLiteOpenHelper;
import android.util.Log;

public class DbCat {

	// Para as variaveis da BD e tabela 'cat'
	public static final String COL_id = "id";
	public static final String COL_nome = "nome";
	public static final String COL_descricao = "descricao";

	// Para o nome da bd
	private static final String DATABASE_NAME ="Categorias";
	// Para o nome da tabela da bd
	private static final String DATABASE_TABLE ="cat";
	private static final int VersaoDB = 2;

	// Instancia
	private DbHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;

	private static class DbHelper extends SQLiteOpenHelper {
		private static final String TAG = null;
		public DbHelper(Context context) {
			super(context, DATABASE_NAME, null, VersaoDB);

		}
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE if not exists " + DATABASE_TABLE + " (" + 
					COL_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
					COL_nome + " TEXT NOT NULL, " + 
					COL_descricao + " TEXT NOT NULL);"
					);
			Log.v("INFO1","creating db");
		}
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
		                + newVersion + ", which will destroy all old data");
		        db.execSQL("DROP TABLE IF EXISTS log");
		        onCreate(db);
		}
	}
	// Construtor
	public DbCat(Context c) {
		ourContext = c;
	}
	public DbCat open() throws SQLException {
		ourHelper = new DbHelper(ourContext);
		ourDatabase = ourHelper.getWritableDatabase();
		return this;
	}
	public void close(){
		ourHelper.close();
	}
	
	public String getDados() {
		String [] columns = new String[] {COL_id, COL_nome, COL_descricao};
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		String dadosLidos = "";
		int linhaId = c.getColumnIndex(COL_id);
		int linhaNome = c.getColumnIndex(COL_nome);
		int linhaDes = c.getColumnIndex(COL_descricao);
		
		for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
		{
			dadosLidos = dadosLidos + c.getString(linhaId)+ " " + c.getString(linhaNome) + " " + c.getString(linhaDes) + "\n";
		}
		return null;
	}
	
	public boolean checkTabela() {
	    Cursor c = ourDatabase.rawQuery("SELECT  * FROM " + DATABASE_TABLE, null);
	    if (!(c.moveToFirst()) || c.getCount() ==0){
	    	return false;
	    }
		return true;
	}
	public long criarRegisto(String name, String desc) {
		Log.d("test", "adding");
		ContentValues cv = new ContentValues();
		cv.put(COL_nome, name);
		cv.put(COL_descricao, desc);
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
		
	}
}



My java class
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class SQLVIEWCAT extends SherlockActivity {
	
	Intent intent;
	com.actionbarsherlock.view.ActionMode mMode;
	com.actionbarsherlock.view.ActionMode.Callback mCallback;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqlviewcat);
        TextView tv = (TextView)findViewById(R.id.tvSQLinfo);
        DbCat info = new DbCat(this);
        info.open();
        String data = info.getDados();
        info.close();
        tv.setText(data);
        
        
        mCallback = new Callback() {
			
        	/** Invoked whenever the action mode is shown. This is invoked immediately after onCreateActionMode */
			@Override
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {				
				return false;
			}
			
			/** Called when user exits action mode */
			@Override
			public void onDestroyActionMode(ActionMode mode) {	
				finish();
			}
			
			/** This is called when the action mode is created. This is called by startActionMode() */
			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				mode.setTitle("Categorias");
                getSupportMenuInflater().inflate(R.menu.sherlock_m_normal, menu);
                return true;
			}
			
			
			/** This is called when an item in the context menu is selected */
			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				switch(item.getItemId()){
	                case R.id.novo:
	                	Intent cat = new Intent(SQLVIEWCAT.this, SQLINSERTCAT.class);
	        			startActivity(cat);
	                        mode.finish();  // Automatically exists the action mode, when the user selects this action
	                        break;
	                case R.id.editar:
	                        Toast.makeText(getBaseContext(), "Escolheu editar ", Toast.LENGTH_LONG).show();
	                        break;
	                case R.id.apagar:
	                        Toast.makeText(getBaseContext(), "Escolheu apagar ", Toast.LENGTH_LONG).show();
	                        break;
				}
				return false;

			}
		};
		mMode = startActionMode(mCallback);		
    }
   
	@Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    	getSupportMenuInflater().inflate(R.menu.sherlock_m_normal, menu);
    	return super.onCreateOptionsMenu(menu);
    }
    
}



and finally my layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <TableRow>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Nome" >
            </TextView>
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Descrição" >
            </TextView>
        </TableRow>
    </TableLayout>

    <TextView
        android:id="@+id/tvSQLinfo"
        android:layout_width="fill_parent"
        android:layout_height="211dp"
        android:layout_gravity="center"
        android:text="Informação da BD" >

    </TextView>
</LinearLayout>



I can use the function to update the database since i receive a toast message saying "recorded" but when i open the activity to show me the records....everything is empty?

Anything wrong in this code?

Thanks

Is This A Good Question/Topic? 0
  • +

Page 1 of 1