Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

New Forums: Nexus 6 | Nexus Player | Nexus 9
test: Reply
 
LinkBack Thread Tools
Old February 23rd, 2011, 09:25 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Cry Help for app with SQLite -graphical table?

Hi
I have a big problem and I am new in Andoid development applications.
The problem is :I have to make app which create a dinamicaly table (for Students) to add Student and delete students and to show garphicaly this table.And every time when I delete Student or add one the table graphically to delete the same row from the table or to add new row.(in the layout may be).
I create some code but when I run the application It stop unespectedly.I put the code.Please help!!Thank you .
P.S I hope you inderstand what the application is about.If you do not understand something just ask me .

Advertisements
stanimira3 is offline  
Reply With Quote
sponsored links
Old February 23rd, 2011, 09:27 AM   #2 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is the core for class HelloTable.java
package bg.example;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class HelloTable extends Activity {
private Button create;
private Button show;
private Button add;
private Button addobj;
public EditText text1;
public EditText text2;
public EditText text3;
public EditText text4;
private Button editobj;
private Button deleteobj;
DBAdapter db = new DBAdapter(this);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
create=(Button)findViewById(R.id.create);
show = (Button) findViewById(R.id.show);
addobj=(Button) findViewById(R.id.addobj);
add=(Button) findViewById(R.id.add);
deleteobj=(Button) findViewById(R.id.delete);
editobj=(Button) findViewById(R.id.edit);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findViewById(R.id.enterLayout).setVisibility(View. GONE);
findViewById(R.id.outputLayout).setVisibility(View .VISIBLE);}});
addobj.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findViewById(R.id.enterLayout).setVisibility(View. GONE);
findViewById(R.id.addLayout).setVisibility(View.VI SIBLE);}});
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text1 = (EditText) findViewById(R.id.entry1);
String abrtext1=text1.getText().toString();
text2 = (EditText) findViewById(R.id.entry2);
String abrtext2=text2.getText().toString();
Integer abrtext= Integer.parseInt(abrtext2);
text3 = (EditText) findViewById(R.id.entry3);
String abrtext3=text3.getText().toString();
text4 = (EditText) findViewById(R.id.entry4);
String abrtext4=text4.getText().toString();
db.open();
long id;
id = db.insertStudent(
abrtext1,abrtext,abrtext3,abrtext4);
db.close();
findViewById(R.id.addLayout).setVisibility(View.GO NE);
findViewById(R.id.enterLayout).setVisibility(View. VISIBLE);}});
deleteobj.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findViewById(R.id.addLayout).setVisibility(View.GO NE);
findViewById(R.id.deleteLayout).setVisibility(View .VISIBLE);
db.open();

text2 = (EditText) findViewById(R.id.entry2);
String abrtext2=text2.getText().toString();
Integer fn=Integer.parseInt(abrtext2);
db.deleteStudent(fn);
db.close();

}});
editobj.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findViewById(R.id.outputLayout).setVisibility(View .GONE);
findViewById(R.id.addLayout).setVisibility(View.VI SIBLE);}});
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text1 = (EditText) findViewById(R.id.entry1);
String abrtext1=text1.getText().toString();
text2 = (EditText) findViewById(R.id.entry2);
String abrtext2=text2.getText().toString();
Integer abrtext= Integer.parseInt(abrtext2);
text3 = (EditText) findViewById(R.id.entry3);
String abrtext3=text3.getText().toString();
text4 = (EditText) findViewById(R.id.entry4);
String abrtext4=text4.getText().toString();

db.open();

db.updateStudent(abrtext1,abrtext,abrtext3,abrtext 4);
db.close();
}});

//mtranslate.setOnClickListener(new View.OnClickListener() {
//public void onClick(View v) {
//here have to be not secondwindow.class but for data base activity
//Intent i = new Intent(SecondWindow.this, FullName.class);
//startActivity(i);
show.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
findViewById(R.id.enterLayout).setVisibility(View. GONE);
findViewById(R.id.outputLayout).setVisibility(View .VISIBLE);

//---get all students---
db.open();
Cursor c = db.getAllStudents();
if (c.moveToFirst())
{
do {
DisplayStudents(c);
} while (c.moveToNext());
}
db.close();

// Intent i = new Intent(HelloTable.this, TableActivity.class);
//startActivity(i);
}}); }
public void DisplayStudents(Cursor c){
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"ISBN: " + c.getString(1) + "\n" +
"TITLE: " + c.getString(2) + "\n" +
"PUBLISHER: " + c.getString(3),
Toast.LENGTH_LONG).show();
}
}
stanimira3 is offline  
Reply With Quote
Old February 23rd, 2011, 09:29 AM   #3 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

this is the helping class DBAdapter.java
package bg.example;
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 DBAdapter {
//constant from type String

public static final String KEY_NAME = "name";
public static final String KEY_FN = "fnumber";
public static final String KEY_CURS="curs";
public static final String KEY_SPEZ = "spezialnost";
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "studentsSU";
private static final String DATABASE_TABLE = "students";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table titles (fnumber integer primary key not null, "
+ "name text not null, fnumber text not null, curs text not null "
+ "spezialnost text not null);";

private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@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 titles");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a student into the database---
public long insertStudent(String name, Integer fn, String curs,String spez)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_FN, fn);
initialValues.put(KEY_CURS, curs);
initialValues.put(KEY_SPEZ, spez);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular student---
public boolean deleteStudent(Integer rowId)
{
return db.delete(DATABASE_TABLE, KEY_FN +
"=" + rowId, null) > 0;
}
//---retrieves all the Students---
public Cursor getAllStudents()
{
return db.query(DATABASE_TABLE, new String[] {

KEY_NAME,
KEY_FN,
KEY_CURS,
KEY_SPEZ },
null,
null,
null,
null,
null
);
}
//---retrieves a particular Student---
public Cursor getStudent(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_NAME,
KEY_FN,
KEY_CURS,
KEY_SPEZ
},
KEY_FN + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a Student---
public boolean updateStudent( String name,
Integer fn, String curs,String spezialnost)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_FN, fn);
args.put(KEY_CURS, curs);
args.put(KEY_SPEZ, spezialnost);
return db.update(DATABASE_TABLE, args,
KEY_FN + "=" + fn, null) > 0;
}
}
stanimira3 is offline  
Reply With Quote
Old February 23rd, 2011, 09:31 AM   #4 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

the main.xml code :

<?
xmlversion="1.0"encoding="utf-8"?>
<
RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>


<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/enterLayout"

android:visibility="visible">
<
Button

android:text
="CREATE TABLE"

android:layout_width
="fill_parent"

android:layout_height
="wrap_content"

android:id
="@+id/create"

android:textStyle
="bold"

></
Button>

<
Buttonandroid:text="SHOW"

android:layout_width
="fill_parent"

android:layout_height
="wrap_content"

android:id
="@+id/show"

android:textStyle
="bold"

android:layout_below
="@+id/create"

></
Button>

</
RelativeLayout>

<
RelativeLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/outputLayout"

android:visibility="gone"

>

<
Button

android:text
="ADD OBJECT"

android:layout_width
="fill_parent"

android:layout_height
="wrap_content"

android:id
="@+id/addobj"

android:textStyle
="bold"></Button>
<
Button

android:text
="DELETE OBJECT"

android:layout_width
="fill_parent"

android:layout_height
="wrap_content"

android:textStyle
="bold"

android:id
="@+id/delete"

android:layout_below
="@+id/add"></Button>
<
Button

android:text
="EDIT OBJECT"

android:layout_width
="fill_parent"

android:layout_height
="wrap_content"

android:id
="@+id/edit"

android:textStyle
="bold"

android:layout_below
="@+id/delete"></Button>

</
RelativeLayout>
<
RelativeLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/addLayout"

android:visibility="gone">

<
TableLayout

android:id="@+id/table"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:stretchColumns="*"

android:background="#ff0000">
<
TableRow
android:layout_margin="1dp">
<
TextView
android:text="Name"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"/>

<EditText
android:id="@+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
/>

</
TableRow>

<
TableRow
android:layout_margin="1dp">

<TextView
android:text="FN"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"/>

<EditText
android:id="@+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
/>
</TableRow>

<
TableRow

android:layout_margin="1dp">

<TextView
android:text="Kurs"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"/>


<EditText
android:id="@+id/entry3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>
</TableRow>

<
TableRow

android:layout_margin="1dp">

<TextView
android:text="Spezialnost"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"/>

<EditText
android:id="@+id/entry4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>


</TableRow>
</
TableLayout>

<Button
android:layout_below="@id/table"

android:id="@+id/add"

android:text="OK"
android:layout_width="200dip"

android:layout_height="80dip"
android:textStyle="bold"

android:layout_marginLeft="240dip">
</
Button>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/deleteLayout"

android:visibility="gone"

>
<
TextView
android:text="Enter Fakultenet nomer:"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"

android:id="@+id/text"/>
<
EditText
android:id="@+id/fntext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@+id/text"/>


<
Button

android:text
="delobj"

android:layout_width
="fill_parent"

android:layout_height
="wrap_content"

android:id
="@+id/delobj"

android:textStyle
="bold"

android:layout_below
="@+id/fntext"></Button>

</
RelativeLayout>

</
RelativeLayout>

stanimira3 is offline  
Reply With Quote
Old February 23rd, 2011, 09:31 AM   #5 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

the table.xml
<?
xmlversion="1.0"encoding="UTF-8"?>
<
TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">

<TableRow>

<TextView
android:layout_column="1"

android:text="#"
android:layout_width="wrap_content"

androidadding="3dip"
android:id="@+id/nomer"
android:layout_height="wrap_content"

android:textSize="20sp"></TextView>


<Viewandroid:id="@+id/View01"
android:background="#FF0000"
android:layout_width="2dip"
android:layout_height="30dp"></View>



<TextView
android:layout_column="2"
android:text="Name"
androidadding="3dip"
android:textSize="20sp"/>

<Viewandroid:id="@+id/View02"
android:background="#FF0000"
android:layout_height="30dp"

android:layout_width="2dip">

</View>

<TextView
android:layout_column="3"
android:text="Student number"
androidadding="3dip"

android:textSize="20sp"/>
<View
android:layout_width="2dip"

android:background="#FF0000"
android:layout_height="30dp"/>

<TextView
android:layout_column="4"
android:text="Major"
androidadding="3dip"
android:textSize="20sp"/>

</
TableRow>
<View
android:id="@+id/View03"
android:layout_width="wrap_content"
android:layout_height="2dip"
android:background="#FF0000"></View>

</TableLayout>
stanimira3 is offline  
Reply With Quote
Old February 23rd, 2011, 09:32 AM   #6 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

addobject.xml:
<?
xmlversion="1.0"encoding="UTF-8"?>

<
RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

androidrientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">
<
TableLayout

android:id="@+id/table"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:stretchColumns="*"

android:background="#ff0000">
<
TableRow
android:layout_margin="1dp">
<
TextView
android:text="Name"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"/>

<EditText
android:id="@+id/entry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
/>

</
TableRow>

<
TableRow
android:layout_margin="1dp">

<TextView
android:text="FN"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"/>

<EditText
android:id="@+id/entry2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
/>
</TableRow>

<
TableRow

android:layout_margin="1dp">

<TextView
android:text="Kurs"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"/>


<EditText
android:id="@+id/entry4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>
</TableRow>

<
TableRow

android:layout_margin="1dp">

<TextView
android:text="Spezialnost"
android:background="#000000"

android:layout_margin="1dp"
android:textSize="20sp"/>

<EditText
android:id="@+id/entry5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>


</TableRow>
</
TableLayout>

<Button
android:layout_below="@id/table"

android:id="@+id/add"

android:text="ADD"
android:layout_width="200dip"

android:layout_height="80dip"
android:textStyle="bold"

android:layout_marginLeft="240dip">
</
Button>

</RelativeLayout>
stanimira3 is offline  
Reply With Quote
Old February 23rd, 2011, 09:34 AM   #7 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2011
Posts: 7
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know the code is too much but if somebody can help I thanks alot.
stanimira3 is offline  
Reply With Quote
Old March 5th, 2011, 04:39 AM   #8 (permalink)
New Member
 
Join Date: Mar 2011
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello, I have similar problem. I found an example: Android SQLite Basics: creating and using a database, and working with sqlite3 | Screaming Penguin and at first evrerythin was working fine, but when I tried to change the name of table (from table1 to let's say table123).
I get this error:
03-04 20:31:45.876: ERROR/AndroidRuntime(727): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.demo.notepad1/com.android.demo.notepad1.Notepadv1}: android.database.sqlite.SQLiteException: no such table: table123: , while compiling: SELECT _id, title, body FROM table123

how can I solve this problem? It looks like I can't add new table?
becijas is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 03:26 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.