我的 TextView 没有存储在我的数据库中?
我正在尝试将我的数据库连接到两个 Editviews 和一个 Textview 。编辑视图正在存储,但在存储文本视图时出现问题,我想将我的地理点(纬度和朗度)值作为 TextView 存储在我的数据库中。这是我的代码
public class Mydatabase4meActivity extends Activity {
/** Called when the activity is first created. */
ArrayList<String> al=new ArrayList<String>();
EditText username_edt,pass_edt;
Bitmap b;
Button login_but;
MyDatabase mdb;
ImageView iv;
SQLiteDatabase sqlite;
TextView tv;
Cursor c;
LocationManager mlocManager;
LocationListener mlocListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username_edt=(EditText)findViewById(R.id.editText1);
pass_edt=(EditText)findViewById(R.id.editText2);
login_but=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.textView1);
// text view
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
for (String provider: mlocManager.getAllProviders()) {
System.out.println(provider);
Location loc = mlocManager.getLastKnownLocation(provider);
if (loc != null) {
Double latitude = loc.getLatitude();
Double longitude = loc.getLongitude();
String Text = "Current location for provider "+ provider + ":" + "Lat=" + latitude.toString() + " Long=" + longitude.toString();
//tv.getTag(Text);
tv.setText(Text);
//Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
}
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: "+ "Latitud =" + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
//tv.setText(Text);
tv.setText(Text = "My current location is: "+ "Latitud =" + loc.getLatitude() +
"Longitud = " + loc.getLongitude());
//Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
tv.getText();
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
tv.getText();
Toast.makeText( getApplicationContext(),"Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mlocManager != null) {
mlocManager.removeUpdates(mlocListener);
mlocManager = null;
}
// login button
login_but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mdb=new MyDatabase(getApplicationContext(), "abcdef", null, 1);
sqlite=mdb.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("Employeename", username_edt.getText().toString());
cv.put("password", pass_edt.getText().toString());
cv.put("coordinates", tv.getText().toString());
sqlite.insert("employee", null, cv);
String col[]={"Employeename","password","coordinates"};
c=sqlite.query("employee",col, null, null, null, null, null);
if(c!=null){
c.moveToFirst();
do{
int i=c.getInt(c.getColumnIndex("Employeename"));
al.add(""+i);
String str=c.getString(c.getColumnIndex("coordinates"));
al.add(str);
String str1=c.getString(c.getColumnIndex("password"));
al.add(str1);
}while(c.moveToNext());
}
mdb.close();
sqlite.close();
}
});
Toast.makeText(getApplicationContext(), "data is inserted", Toast.LENGTH_SHORT).show();
}
}
,这是我的数据库类
public class MyDatabase extends SQLiteOpenHelper{
public MyDatabase(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
db.execSQL("create table employee(Employeename varchar2(10),password integer(10),coordinates varchar2(7,2));");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
,我的 XML 文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="USERNAME" >
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PASSWORD"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="Login" />
</LinearLayout>
i'm trying to connecting my data base into two Editviews and one Textview . edit-views are storing but problem at storing text views ,i would like to store my Geo points(Lat & Lang) values as TextView in my DataBase.here this is my code
public class Mydatabase4meActivity extends Activity {
/** Called when the activity is first created. */
ArrayList<String> al=new ArrayList<String>();
EditText username_edt,pass_edt;
Bitmap b;
Button login_but;
MyDatabase mdb;
ImageView iv;
SQLiteDatabase sqlite;
TextView tv;
Cursor c;
LocationManager mlocManager;
LocationListener mlocListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username_edt=(EditText)findViewById(R.id.editText1);
pass_edt=(EditText)findViewById(R.id.editText2);
login_but=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.textView1);
// text view
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
for (String provider: mlocManager.getAllProviders()) {
System.out.println(provider);
Location loc = mlocManager.getLastKnownLocation(provider);
if (loc != null) {
Double latitude = loc.getLatitude();
Double longitude = loc.getLongitude();
String Text = "Current location for provider "+ provider + ":" + "Lat=" + latitude.toString() + " Long=" + longitude.toString();
//tv.getTag(Text);
tv.setText(Text);
//Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
}
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: "+ "Latitud =" + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
//tv.setText(Text);
tv.setText(Text = "My current location is: "+ "Latitud =" + loc.getLatitude() +
"Longitud = " + loc.getLongitude());
//Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
tv.getText();
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
tv.getText();
Toast.makeText( getApplicationContext(),"Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mlocManager != null) {
mlocManager.removeUpdates(mlocListener);
mlocManager = null;
}
// login button
login_but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mdb=new MyDatabase(getApplicationContext(), "abcdef", null, 1);
sqlite=mdb.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("Employeename", username_edt.getText().toString());
cv.put("password", pass_edt.getText().toString());
cv.put("coordinates", tv.getText().toString());
sqlite.insert("employee", null, cv);
String col[]={"Employeename","password","coordinates"};
c=sqlite.query("employee",col, null, null, null, null, null);
if(c!=null){
c.moveToFirst();
do{
int i=c.getInt(c.getColumnIndex("Employeename"));
al.add(""+i);
String str=c.getString(c.getColumnIndex("coordinates"));
al.add(str);
String str1=c.getString(c.getColumnIndex("password"));
al.add(str1);
}while(c.moveToNext());
}
mdb.close();
sqlite.close();
}
});
Toast.makeText(getApplicationContext(), "data is inserted", Toast.LENGTH_SHORT).show();
}
}
here this is myDatabase class
public class MyDatabase extends SQLiteOpenHelper{
public MyDatabase(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
db.execSQL("create table employee(Employeename varchar2(10),password integer(10),coordinates varchar2(7,2));");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
my XML file..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="USERNAME" >
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PASSWORD"
android:inputType="textPassword" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="Login" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取一个全局变量字符串。
现在您正在获取双精度类型的纬度和经度值。所以你可以将 then 存储在字符串中。
1)如果数据库中有一个经纬度字段,如下所示:
按如下方式插入到数据库中:
cv.put(“坐标”,strlatlong);
2)如果数据库中有两个字段,那么您可以使用如下:
插入到数据库中,如下所示:
您应该更改数据库字段。
上面的表更改将允许您使用上面的代码插入到表中。
Take a global variable string.
Now you are taking values of latitude ans longitude in double type. So you can store then in a string.
1) if you have a single field of lat-long in database as follows:
insert into db as follows:
cv.put("coordinates",strlatlong);
2) if you have two fields in database then you can use as follows:
insert into db as follows:
You should change your database fields.
Above table changes will let you insert into table using above code.