Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am an android beginner....coding a very simple app...which takes in an employee id,name and stores it in a database...and shows the details when "DISPLAY" is clicked.

there is no error when i click the save button..but it shows "UNFORTUNATELY THE APP HAS STOPPED" when i click display..please help...here is my code

Java
/* THIS IS MAINACTIVITY .JAVA */

package com.example.emp_db;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.database.Cursor;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity {

	static SQLiteDatabase db= null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Myhelper mdh =new Myhelper(this,"employeedb",null,1) ;
		db=mdh.getWritableDatabase();
		
		Button save =(Button)findViewById(R.id.button1);
		Button disp=(Button)findViewById(R.id.button2);
		
		
		save.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			EditText eid_us = (EditText)findViewById(R.id.edittext_id)	;
			EditText ename_us =(EditText)findViewById(R.id.editText_name) ;
			
			int eid = 22;//Integer.parseInt(eid_us.getText().toString());
			String ename ="madhuri";//ename_us.getText().toString();
			
			ContentValues cv =new ContentValues();
			cv.put("eno", eid);
			cv.put("ename", ename);
			
			db.insert("employee", null, cv);
			
			}
		});
		
		disp.setOnClickListener(new View.OnClickListener() {
		
			
			

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			String[] col=new String[] {"eno","ename"};
			Cursor c= db.query("employee", col, null, null, null, null, null);
			String eno_db;
			 if(c.getString(1)!="") eno_db=c.getString(1); else
			  eno_db="it is null";
			 TextView t1=(TextView)findViewById(R.id.text_display);
				 t1.setText(eno_db);
			}
		});
	
    
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

*************************************************************
/*           THIS IS MYHELPER.JAVA           */
package com.example.emp_db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class Myhelper extends SQLiteOpenHelper {

	public Myhelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String str="create table employee(eno integer , ename text)";
		db.execSQL(str);
	}
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
	}
}
******************************************************************************************
*/ THIS IS ACTIVITY_MAIN.XML */

HTML
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_centerVertical="false"
        android:text="@string/emp_id"
        android:textSize="40sp" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/edittext_id"
        android:layout_alignTop="@+id/button1"
        android:text="@string/str_display" />

    <textview>
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/edittext_id"
        android:layout_marginLeft="61dp"
        android:layout_marginTop="26dp"
        android:text="@string/emp_name"
        android:textSize="30sp" />

    <edittext>
        android:id="@+id/editText_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:ems="10"
        android:inputType="textPersonName" >
        <requestfocus />
    </edittext>

    <edittext>
        android:id="@+id/edittext_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/editText_name"
        android:layout_marginTop="26dp"
        android:text="@string/str_save" />

    <textview>
        android:id="@+id/text_display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText_name"
        android:layout_below="@+id/button1"
        android:layout_marginTop="55dp"
        android:textSize="40sp" />
</textview></edittext></textview></textview></relativelayout>

*******************************************************************************************
Posted
Updated 26-Jun-13 23:48pm
v2
Comments
Sudhakar Shinde 27-Jun-13 5:50am    
Done the code formatting please check XML is correct.
maddy_bitsp 27-Jun-13 13:42pm    
@sudhakar..thank u.. but when i copied the above xml code.... i got ""[2013-06-27 23:08:42 - emp_db] Error in an XML file: aborting build "" error
Sudhakar Shinde 28-Jun-13 1:12am    
I did not do any changes just formatted the code. Original XML which you have posted is unchanged but I think it is not correct.
maddy_bitsp 28-Jun-13 2:04am    
but i dont think there is any problem with my xml bcoz when i replace "c.getString(1)" with "c.getColumnName(1)" in mainActivity.java ...it is working fine and showing me the column name...and i am also sure that the data is being entered in the database correctly bcoz i checked it using file explorer

1 solution

check this link:

basic-databse-operations-in-sqlite.html[^]


check the article in above link and see how cursor is using.
 
Share this answer
 
Comments
maddy_bitsp 28-Jun-13 12:42pm    
i fixed my code..it is that the curor index is at -1 initially..so when i used c.moveToNext() twice and then used c.getString() ...it worked...but i do not know why the cursor is at -1 initially...can u help please
josh-jw 29-Jun-13 0:36am    
show ur fixed code
maddy_bitsp 29-Jun-13 16:08pm    
//MAIN_ACTIVITY.JAVA

package com.example.emp_db;

import android.os.Bundle;



import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.database.Cursor;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.ContentValues;


import android.database.sqlite.SQLiteDatabase;


public class MainActivity extends Activity {

static SQLiteDatabase db= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Myhelper mdh =new Myhelper(this,"employeedb",null,1) ;
db=mdh.getWritableDatabase();
db.delete("employee", null, null);
Button save =(Button)findViewById(R.id.button1);
Button disp=(Button)findViewById(R.id.button2);


save.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText eid_us = (EditText)findViewById(R.id.edittext_id) ;
EditText ename_us =(EditText)findViewById(R.id.editText_name) ;

int eid = Integer.parseInt(eid_us.getText().toString());
String ename =ename_us.getText().toString()+"";

ContentValues cv =new ContentValues();
cv.put("eno", eid);
cv.put("ename", ename);

db.insert("employee", null, cv);

}
});

disp.setOnClickListener(new View.OnClickListener() {




@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String[] col=new String[] {"eno","ename"};
Cursor c= db.query("employee", col, null, null, null,null,null);
//c.moveToNext();
String eno_db="";
// c.moveToNext();
// c.moveToNext();
c.moveToFirst();
//c.moveToLast();

eno_db=c.getString(1);
c.close();
// db.close();
TextView t1=(TextView)findViewById(R.id.text_display);
t1.setText(eno_db);
}
});



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
josh-jw 30-Jun-13 23:57pm    
if (cursor.moveToFirst()) {
do {
eno_db=c.getString(1);
} while (cursor.moveToNext());
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900