Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in this i have two button save and load,so when i press the save button the data inserted but i want to that when i press the load button so then the data which is inserted by save button should to display in second edittext.the data saved loaded from the first edittext but should to load by pressing load button to second edittext



Java
package com.example.database;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener{
	Button save,load;
	EditText name,email;
	DataHaandler handler;
	SensorManager sm;
	Sensor accelerometer;
	

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        save=(Button)findViewById(R.id.save);
        load=(Button)findViewById(R.id.load);
        name=(EditText)findViewById(R.id.name);
        
        sm=(SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        
        save.setOnClickListener(new View.OnClickListener() {
			
			@Override
		public void onClick(View arg0) {
				// TODO Auto-generated method stub
			String getName=name.getText().toString();
			
			handler=new DataHaandler(getBaseContext());
			handler.open();
			long id=handler.insertData(getName,null);
			Toast.makeText(getBaseContext(), "data inserted", Toast.LENGTH_SHORT).show();
			handler.close();
			
			
			
			
			}
		});
        
       // load.setOnClickListener(new View.OnClickListener() {
			
			//@Override
			//public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				String getName1=name.getText().toString();
			
				
				handler=new DataHaandler(getBaseContext());
				handler.open();
				Cursor C= handler.returnData();
				if(C.moveToFirst())
				{
					do
					{
					getName1=C.getString(0);
					
					}while(C.moveToNext());
				}
				handler.close();
				Toast.makeText(getBaseContext(), ""+getName1, Toast.LENGTH_SHORT).show();
			}
		//});
        
  // }


    @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;
    }


	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		name.setText("X: "+event.values[0]+
	            "\nY:"+event.values[1]+
	            "\nZ:"+event.values[2]);
	}
    
}
 class of database

package com.example.database;

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 DataHaandler {
	
	public static final String NAME="name";
	public static final String EMAIL="email";
	public static final String TABLE_NAME="mytable";
	public static final String DATA_BASE_NAME="mydatabase";
	public static final int DATABASE_VERSION=1;
	public static final String TABLE_CREATE="create table mytable(name text not null,email text not null);";
	
	DataBaseHelper dbhelper;
	Context ctx;
	SQLiteDatabase db;
	public  DataHaandler(Context ctx)
	{
		this.ctx=ctx;
		dbhelper=new DataBaseHelper(ctx);
	}
	private static class DataBaseHelper extends SQLiteOpenHelper
	{
		public DataBaseHelper(Context ctx)
		{
		super(ctx,DATA_BASE_NAME,null,DATABASE_VERSION);	
		}

		public DataBaseHelper(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
		try{
			db.execSQL(TABLE_CREATE);
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			
		db.execSQL("DROP TABLE IF EXISTS mytable");
		onCreate(db);
			
		}
		
	}
	public DataHaandler open()
	{
		db=dbhelper.getWritableDatabase();
		return this;
	}
	
	public void close()
	{
		dbhelper.close();
	}
	
	public long insertData(String name,String email)
	{
		ContentValues content=new ContentValues();
		content.put(name, name);
		content.put(email, email);
		return db.insert(TABLE_NAME, null, content); 
	}
	
	public Cursor returnData()
	{
		return db.query(TABLE_NAME, new String[]{NAME,EMAIL}, null, null,null, null, null);
		
	}


}
Posted
Updated 26-Jul-15 17:13pm
v3
Comments
Richard MacCutchan 27-Jul-15 4:55am    
OK, and what is the problem?
wajib rehman 27-Jul-15 8:30am    
the problem is that.that when i clicked the load button the data doesnot load to i mean does not show in the second edit text
wajib rehman 27-Jul-15 8:32am    
i commented the load button but if it its out of this then it also not work

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