根据上面的简要,我们在listview显示界面点击option menu中的insert按钮之后就会跳转到 数据库数据插入编辑界面
此activity是Dialog主题,因其在Androidmenifest.xml文件配置时,theme = “@android:style/Theme.Dialog”
布局文件:add_userdata.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10px"
>
<LinearLayout
android:id="@+id/name_linearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10px"
>
<TextView
android:id="@+id/insert_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
/>
<EditText
android:id="@+id/input_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/number_linearlayout"
android:orientation="horizontal"
android:layout_below="@id/name_linearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
>
<TextView
android:id="@+id/insert_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="号码:"
/>
<EditText
android:id="@+id/input_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/insert_commit"
android:layout_below="@id/number_linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="ADD"
/>
</RelativeLayout>
activity 上代码:
package com.example.listviewdatabase;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class InsertUserEditDialog extends Activity {
private static final String TAG = "ListViewActivity";
private Button commit = null;
private EditText input_name = null;
private EditText input_number = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_userdata);
commit = (Button) findViewById(R.id.insert_commit);
//get id of layout
input_name = (EditText) findViewById(R.id.input_name);
input_number = (EditText) findViewById(R.id.input_number);
//ID of editview is not used wrongly
commit.setOnClickListener(new SetOnClickedListener());
}
class SetOnClickedListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// create dbh
DataBaseHelper dbh = new DataBaseHelper(InsertUserEditDialog.this, null);
Log.i(TAG, "setOnClickListener ---> onClick 1111 dbh = "+dbh);
// get writable database
SQLiteDatabase db = dbh.getWritableDatabase();
Log.i(TAG, "setOnClickListener ---> onClick 2222");
// get context of edit
String name = input_name.getText().toString();
String number = input_number.getText().toString();
if(db == null){
Log.i(TAG, "null");
}
Log.i(TAG, "SetOnClickedListener --->onClick name = "+name+", number = "+number);
// insert
dbh.insertDataBase(db, name, number);
//close
db.close();
dbh.close();
// back to listview
InsertUserEditDialog.this.finish();
}
}
}
此文章是本人学习笔记,能力有限,相互学习,欢迎点评,谢谢...