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();
}
}
这是我的数据库的代码,我必须从中获取存储在数据库中的lat和lng值,并在我的主要活动中使用它。我无法编写任何可以实现它的方法。请帮我编写所需的方法。我是android的新手......
答案 0 :(得分:1)
您可以使用例如intent extra将数据发送到另一个Activity: 你可以阅读这篇文章: How to use putExtra() and getExtra() for string data
另一种方法是将数据设置为Application类的变量(有很多方法可以实现): 其中一个你可以在这里阅读Using the Android Application class to persist data