2012-02-08 47 views
0

我试图连接我的数据库分成两个Editviews和一个TextView的。编辑视图存储,但在存储文本视图的问题,我想我的存储地理点(纬度&郎)值的TextView在我DataBase.here这是我的代码我的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> 
+0

谁没有时间阅读你的代码量巨大?你应该具体吗? – Sameer 2012-02-08 10:06:31

+0

其中u得到问题 – user1089679 2012-02-08 10:40:37

+0

这里我想我的坐标存储为一个TextView ...它没有工作@ user1089679 – Crishnan 2012-02-08 10:45:31

回答

0

以一个全局变量的字符串。

String strlatlong = null; 

现在你正在采取纬度和经度的双值类型的值。所以你可以把它存储在一个字符串中。 1)如果在数据库具有LAT-长的单个字段如下:

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

插入件插入分贝如下:

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

2)如果你在数据库中的两个字段,那么你可以使用如下:

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

插入到数据库如下:

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);"); 

} 

上表更改将允许您使用上述代码插入到表中。

+0

海.... @ Dhruvisha纬度,郎值R得到,但它并没有在数据的基础上 – Crishnan 2012-02-09 06:47:19

+0

更新ANS存储。请尝试。 – Dhruvisha 2012-02-09 06:55:31

相关问题