oncreatedialog()与showdialog()的使用

package com.lin.db;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;

import com.example.chat1.R;

public class UserActivity extends ListActivity {
private DBOpration db;

@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	db=new DBOpration(this);
	db.openOrCreateDB();
	display();
}

private void display() {
	Cursor c=db.selectAll();
	SimpleCursorAdapter sa=new SimpleCursorAdapter(this,R.layout.dbactivity,
			c, new String[]{
			"name","address"}, new int[]{R.id.textView1,R.id.textView2});
	setListAdapter(sa);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
	// TODO Auto-generated method stub
	if(item.getTitle()=="添加"){
	<span style="color:#ff0000;">showDialog(123);</span>
	}
	return super.onMenuItemSelected(featureId, item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
	// TODO Auto-generated method stub
	menu.add(1, 1, 1, "添加");
	menu.add(1,2,2,"查询");
	return super.onCreateOptionsMenu(menu);
	
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
	// TODO Auto-generated method stub
	
	<span style="color:#ff0000;">if(id==123</span>){
		AlertDialog ad=null;
		ad=new AlertDialog.Builder(this).setTitle("添加")
				.setView(LayoutInflater.from(this).inflate(R.layout.dbdialog, null)).
				setPositiveButton("确定", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						AlertDialog aa=(AlertDialog) dialog;
						EditText name1=(EditText)aa.findViewById(R.id.name1);
						EditText address1=(EditText)aa. findViewById(R.id.address1);
						User user=new User(name1.getText().toString(), address1.getText().toString());
			           db.insert(user);
						display();
						
					}
				}).create();
		ad.show();
	}
	return super.onCreateDialog(id);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值