我的TextView没有存储在我的DataBase中?

时间:2012-02-08 10:00:05

标签: android android-layout android-emulator android-ndk

我正在尝试将我的数据库连接到两个Editview和一个Textview。编辑视图存储但存储文本视图存在问题,我想将我的Geo点(Lat& Lang)值存储为我的DataBase中的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();

    }
}

这是myDatabase类

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>

1 个答案:

答案 0 :(得分:0)

获取全局变量字符串。

String strlatlong = null;

现在你在双重类型中获取纬度和经度的值。所以你可以在一个字符串中存储。  1)如果您在数据库中有一个lat-long字段,如下所示:

strlatlong = String.valueOf(latitude)+" "+String.valueOf(longitude);

按如下方式插入db:

cv.put( “坐标”,strlatlong);

2)如果数据库中有两个字段,则可以按如下方式使用:

 String strlat = null;
 String strlong = null;
strlat = String.valueOf(latitude);
strlong =String.valueOf(longitude);

按如下方式插入db:

cv.put("coordinate_lat",strlat);
cv.put("coordinate_long",strlong);

您应该更改数据库字段。

public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("create table employee(Employeename varchar2(10),password integer(10),coordinate_lat varchar,coordinate_long varchar);");

}

上面的表格更改将允许您使用上面的代码插入表格。