我已在我的主Activity
中创建了数据库而没有DBHelper
类。我添加了布局legend.xml
。如果我运行应用程序,我只能查看我的legend.xml
页面设计,如果单击on按钮,它应该将insert mtd值存储到db table。我已在命令提示符下检查它并创建了我的表,但是没有为按钮单击存储该值。我想要的是,如果用户单击列表中的on按钮,其状态必须存储在db上。只有我需要在地图视图上看到的相关位置处于打开状态的按钮。我也是数据库概念周,请给我解决方案并尽可能发布示例代码
Java代码:( mainactivity)
import java.util.Locale;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class edb extends Activity {
private SQLiteDatabase db;
private String table_name="emptable";
private String database_name="EMP.db";
private static String type;
private static int id;
public static final int KEY_ROWID = id;
public static final String KEY_TYPE = type;
public static final String KEY_STATUS = "status";
public static final String KEY_PLACE = "place";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView arc=(TextView)findViewById(R.id.arctxt);
arc.setText("Architectural \n Integration");
db = openOrCreateDatabase(database_name , SQLiteDatabase.CREATE_IF_NECESSARY,
null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
// creating table in database
db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+" " +
"( KEY_ROWID INTEGER PRIMARY KEY AUTOINCREMENT," +
" KEY_TYPE TEXT,"+ " KEY_STATUS TEXT,"+ "KEY_PLACE TEXT ); ");
}
public void onClick(View v)
{
// capture button id and perform function
switch(v.getId())
{
case R.id.sclpbtnOn:
// db=getWritableDatabase();
// name=et1.getText().toString();
Insert( "sculpture", "ON", "");
break;
case R.id.arcbtnOn:
Insert( "Architectural Integration", "ON", "");
break;
case R.id.histbtnOn:
Insert( "Historical Site", "ON" , "");
break;
case R.id.landbtnOn:
Insert( "Landscape", "ON", "");
break;
case R.id.muralbtnOn:
Insert( "Mural", "ON", "");
break;
}
}
// inserting record in the database
public void Insert( String name, String age, String course)
{
ContentValues data=createContentValues(name, age, course);
// db.insertOrThrow(table_name, null, data);
db.insert(table_name, null, data);
Toast.makeText(this, "button state is on", Toast.LENGTH_SHORT).show();
}
// updating record in the database
/* public void Update(int id, String name, String age, String course)
{
ContentValues updateValues = createContentValues(name, age, course);
db.update(table_name, updateValues, KEY_ROWID+"="+id, null);
Toast.makeText(this, "Record Updated", Toast.LENGTH_SHORT).show();
}
// deleting record rom the database
public void Delete(int id)
{
db.delete(table_name, KEY_ROWID+"="+id, null);
Toast.makeText(this, "Record Deleted", Toast.LENGTH_SHORT).show();
}*/
private ContentValues createContentValues( String name, String status, String place) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put(KEY_TYPE, name);
values.put(KEY_STATUS, status);
values.put(KEY_PLACE, place);
return values;
}
}
legend.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF">
<TableRow android:id="@+id/tableRow1"
android:layout_marginTop="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:layout_marginTop="-5dp"
android:src="@drawable/sculp"
android:id="@+id/sclpimg"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
<TextView android:text="Sculpture"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="15dp"
android:id="@+id/sclptxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button android:text="ON"
android:id="@+id/sclpbtnOn"
android:layout_width="50dp"
android:layout_height="35dp">
</Button>
<Button android:text="OFF"
android:id="@+id/sclpbtnOff"
android:layout_width="50dp"
android:layout_height="35dp">
</Button>
</TableRow>
<View android:id="@+id/view1"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="2dp">
</View>
<TableRow android:id="@+id/tableRow2"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:src="@drawable/arch"
android:id="@+id/arcimg"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
<TextView android:text=""
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:id="@+id/arctxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button android:text="ON"
android:id="@+id/arcbtnOn"
android:layout_marginTop="10dp"
android:layout_width="50dp"
android:layout_height="35dp">
</Button>
<Button android:text="OFF"
android:id="@+id/arcbtnOff"
android:layout_marginTop="10dp"
android:layout_width="50dp"
android:layout_height="35dp">
</Button>
</TableRow>
<View android:id="@+id/view1"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="2dp"></View>
<TableRow android:id="@+id/tableRow3"
android:layout_marginTop="13dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:src="@drawable/land"
android:id="@+id/landimg"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
<TextView android:text="Landscape"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:id="@+id/landtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button android:text="ON"
android:id="@+id/landbtnOn"
android:layout_width="50dp"
android:layout_height="35dp">
</Button>
<Button android:text="OFF"
android:id="@+id/landbtnOff"
android:layout_width="50dp"
android:layout_height="35dp">
</Button>
</TableRow>
<View android:id="@+id/view1"
android:layout_marginTop="8dp"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="2dp">
</View>
<TableRow android:layout_width="wrap_content"
android:layout_marginTop="18dp"
android:id="@+id/tableRow4"
android:layout_height="wrap_content">
<ImageView android:src="@drawable/mural"
android:layout_marginTop="-5dp"
android:id="@+id/muralimg"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
<TextView android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:id="@+id/muraltxt" android:text="Mural">
</TextView>
<Button android:layout_height="35dp"
android:layout_marginTop="10dp"
android:layout_width="50dp"
android:id="@+id/muralbtnOn"
android:text="ON">
</Button>
<Button android:layout_height="35dp"
android:layout_marginTop="10dp"
android:layout_width="50dp"
android:id="@+id/muralbtnOff"
android:text="OFF">
</Button>
</TableRow>
<View android:id="@+id/view1"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="2dp">
</View>
<TableRow android:layout_width="wrap_content"
android:layout_marginTop="16dp"
android:id="@+id/tableRow5"
android:layout_height="wrap_content">
<ImageView android:src="@drawable/historic"
android:id="@+id/histimg"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ImageView>
<TextView android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:id="@+id/histimg"
android:text="Historic Sites">
</TextView>
<Button android:layout_height="35dp"
android:layout_marginTop="10dp"
android:layout_width="50dp"
android:id="@+id/histbtnOn"
android:text="ON">
</Button>
<Button android:layout_height="35dp"
android:layout_marginTop="10dp"
android:layout_width="50dp"
android:id="@+id/histbtnOff"
android:text="OFF">
</Button>
</TableRow>
</TableLayout>