mardi 21 juillet 2015

How to get values from two columns in sqlite android and use it in another activity

package com.OneTouch.locationprovider;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Contact extends Activity implements OnClickListener
{
  EditText editName,editLat,editLng,editnumber;
  Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
  SQLiteDatabase db;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_contact);
     editName=(EditText)findViewById(R.id.editName);
      editLat=(EditText)findViewById(R.id.editLat);
      editLng=(EditText)findViewById(R.id.editLng);
      editnumber=(EditText)findViewById(R.id.editnumber);
      btnAdd=(Button)findViewById(R.id.btnAdd);
      btnDelete=(Button)findViewById(R.id.btnDelete);
     // btnModify=(Button)findViewById(R.id.btnModify);
     btnView=(Button)findViewById(R.id.btnView);
      btnViewAll=(Button)findViewById(R.id.btnViewAll);
     // btnShowInfo=(Button)findViewById(R.id.btnShowInfo);
     btnAdd.setOnClickListener(this);
     btnDelete.setOnClickListener(this);
     // btnModify.setOnClickListener(this);
      btnView.setOnClickListener(this);
      btnViewAll.setOnClickListener(this);
     // btnShowInfo.setOnClickListener(this);
      db=openOrCreateDatabase("ContactsDB", Context.MODE_PRIVATE, null);
      db.execSQL("CREATE TABLE IF NOT EXISTS contacts(name VARCHAR,lat VARCHAR,lng VARCHAR,eid VARCHAR);");
  }
  public void onClick(View view)
  {
      if(view==btnAdd)
      {
          if(editName.getText().toString().trim().length()==0||
             editLat.getText().toString().trim().length()==0||
                     editLng.getText().toString().trim().length()==0||
             editnumber.getText().toString().trim().length()==0)
          {
              showMessage("Error", "Please enter all values");
              return;
          }
          db.execSQL("INSERT INTO contacts VALUES('"+editName.getText()+"','"+editLat.getText()+"','"+editLng.getText()+
                    "','"+editnumber.getText()+"');");
          showMessage("Success", "Record added");
          clearText();
      }
      if(view==btnDelete)
      {
          if(editName.getText().toString().trim().length()==0)
          {
              showMessage("Error", "Please enter Name");
              return;
          }
          Cursor c=db.rawQuery("SELECT * FROM contacts WHERE name='"+editName.getText()+"'", null);
          if(c.moveToFirst())
          {
              db.execSQL("DELETE FROM contacts WHERE name='"+editName.getText()+"'");
              showMessage("Success", "Record Deleted");
          }
          else
          {
             showMessage("Error", "Invalid Name");
          }
          clearText();
      }
      if(view==btnModify)
     {
          if(editName.getText().toString().trim().length()==0)
          {
              showMessage("Error", "Please enter Name");
              return;
          }
          Cursor c=db.rawQuery("SELECT * FROM contacts WHERE name='"+editName.getText()+"'", null);
         if(c.moveToFirst())
          {
              db.execSQL("UPDATE contacts SET lat='"+editLat.getText()+"',lng='"+editLng.getText()+"',marks='"+editnumber.getText()+
                      "' WHERE name='"+editName.getText()+"'");
              showMessage("Success", "Record Modified");
          }
          else
         {
              showMessage("Error", "Invalid Name");
         }
         clearText();
      }
      if(view==btnView)
      {
          if(editName.getText().toString().trim().length()==0)
          {
              showMessage("Error", "Please enter Name");
              return;
          }
          Cursor c=db.rawQuery("SELECT * FROM contacts WHERE name='"+editName.getText()+"'", null);
          if(c.moveToFirst())
          {
              editLat.setText(c.getString(1));
              editLng.setText(c.getString(2));
              editnumber.setText(c.getString(3));
          }
          else
          {
              showMessage("Error", "Invalid Name");
              clearText();
          }
      }
      if(view==btnViewAll)
      {
          Cursor c=db.rawQuery("SELECT * FROM contacts", null);
          if(c.getCount()==0)
          {
              showMessage("Error", "No records found");
              return;
          }
          StringBuffer buffer=new StringBuffer();
         while(c.moveToNext())
          {
              buffer.append("Name: "+c.getString(0)+"\n");
              buffer.append("Lat: "+c.getString(1)+"\n");
              buffer.append("Lng: "+c.getString(2)+"\n");
              buffer.append("Number: "+c.getString(3)+"\n\n");
          }
          showMessage("Contact Details", buffer.toString());
      }
      if(view==btnShowInfo)
      {
          showMessage("Contacts DataBase", "Developed for OneTouch");
      }
  }
  public void showMessage(String title,String message)
  {
      Builder builder=new Builder(this);
      builder.setCancelable(true);
      builder.setTitle(title);
      builder.setMessage(message);
      builder.show();
  }
  public void clearText()
  {
      editName.setText("");
      editLat.setText("");
      editLng.setText("");
      editnumber.setText("");
      editName.requestFocus();
  }

}

This is the code for my database from which i have to get the lat and lng values that is stored in the database and use it in my main activity.I an not able to write any method by which i can achieve it. Please help me with the coding for the required method. I am new to android.....

Aucun commentaire:

Enregistrer un commentaire