//带有输入框的dialog自定义控件
LayoutInflater lf = (LayoutInflater ) LoginActivity.this
.getSystemService(Context. LAYOUT_INFLATER_SERVICE);
ViewGroup vg = (ViewGroup) lf.inflate(R.layout.layout_view , null);
final EditText etShow = (EditText) vg.findViewById(R.id.et_ip );
new AlertDialog.Builder( this)
.setTitle( "请输入ip地址" )
.setIcon(android.R.drawable. ic_dialog_info)
.setView(vg)
.setPositiveButton( "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
String ip = "http://"
+ etShow.getText().toString()
+ ":8080/WeiboServer";
// // 写入服务器地址
HttpDownload httpDownload = new HttpDownload();
httpDownload.write2SDcard(ip, "ip.txt" );
}
}).setNegativeButton( "取消", null ).show();
2014年3月27日星期四
Android使用自定义控件
2014年2月25日星期二
java简单工程模式
实现一个计算器
1.新建计算类
1.新建计算类
public class Operation {
private double _numa;
private double _numb;
private double _result;
/**
* 获取_numa
*
* @return _numa
*/
public double get_numa() {
return _numa;
}
/**
* 设置_numa
*
* @param _numa
*/
public void set_numa(double _numa) {
this._numa = _numa;
}
/**
* 获取_numb
*
* @return _numb
*/
public double get_numb() {
return _numb;
}
/**
* 设置_numb
*
* @param _numb
*/
public void set_numb(double _numb) {
this._numb = _numb;
}
/**
* 获取_result
*
* @return _result
*/
public double get_result() {
return _result;
}
/**
* 设置_result
*
* @param _result
*/
public void set_result(double _result) {
this._result = _result;
}
}
2.新建一个加法类继承OpetationAdd并重写get_result()方法
public class OperationFactory {
/**
* 创建运算类
*/
public static Operation createOperation(String operator) {
Operation operation = null;
switch (operator) {
case "+":
operation = new OperationAdd();
break;
default:
break;
}
return operation;
}
/**
* 描述方法的作用
*
* @param args
*/
public static void main(String[] args) {
Operation op = new OperationFactory().createOperation("+");
op.set_numa(2);
op.set_numb(2);
System.out.println(op.get_result());
}
}
3.新建OperationFactory类并新建createOperation()方法实现抽象运算
public class OperationFactory {
/**
* 创建运算类
*/
public static Operation createOperation(String operator) {
Operation operation = null;
switch (operator) {
case "+":
operation = new OperationAdd();
break;
default:
break;
}
return operation;
}
}
4.测试运行加法
/**
* 描述方法的作用
*
* @param args
*/
public static void main(String[] args) {
Operation op = new OperationFactory().createOperation("+");
op.set_numa(2);
op.set_numb(2);
System.out.println(op.get_result());
}
以后需要修改加法只要修改OperationAdd类即可
如果需要添加其他复杂运算,只要添加相关类并在OperationFactory类的createOperation()方法中添加switch分支即可
2014年2月24日星期一
Android SQLite使用demo
Android SQLite使用demo
1.建立dbhelper类:
Context context;
public static int DB_VERSION = 1;
public static String DB_NAME = "d_weather.db";
public static String TABLE_NAME = "t_weather";
/**
* 构造方法
*
* @param context
* @param name
* @param factory
* @param version
*/
public SQLiteDataHelp(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// SQLiteOpenHelper子类必须要的一个构造函数
}
/**
* 构造方法
*/
public SQLiteDataHelp(Context context) {
// 必须实现父类构造方法
this(context, DB_VERSION);
}
public SQLiteDataHelp(Context context, int version) {
// 必须实现父类构造方法
this(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 数据库初始化
db.execSQL("create table if not exists " + TABLE_NAME + "("
+ "w_id integer primary key," + "w_name varchar,"
+ "w_temp varchar)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 当打开数据库时传入的版本号与当前的版本号不同时会调用该方法
}
public void insert(ContentValues contentValues) {
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, contentValues);
db.close();
}
/**
*
* query
*
* @return
*/
public Cursor query() {
SQLiteDatabase db = getReadableDatabase();
// 获取Cursor
Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null,
null);
return c;
}
//更新数据库的内容
public void update(ContentValues values, String whereClause, String[]whereArgs){
SQLiteDatabase db = getWritableDatabase();
db.update(TABLE_NAME, values, whereClause, whereArgs);
}
// 根据唯一标识_id 来删除数据
public void delete(int id) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_NAME, "w_id=?", new String[] { String.valueOf(id) });
}
2.调用CRUD方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// CRUD操作
// insert
SQLiteDataHelp dbhelp = new SQLiteDataHelp(MainActivity.this);
ContentValues contentValues = new ContentValues();
contentValues.put("w_name", "北京");
contentValues.put("w_temp", "22°C");
// dbhelp.insert(contentValues);
// delete
// dbhelp.delete(1);
// 修改
dbhelp.update(contentValues, "w_name='北京'", null);
// 查询
Cursor cursor = dbhelp.query();
if (cursor.getCount() == 0) {
GlobalConstant.i("no data!!");
} else {
while (cursor.moveToNext()) {
GlobalConstant.i("w_id--->"
+ cursor.getInt(cursor.getColumnIndex("w_id")));
GlobalConstant.i("w_name--->"
+ cursor.getString(cursor.getColumnIndex("w_name")));
GlobalConstant.i("w_temp--->"
+ cursor.getString(cursor.getColumnIndex("w_temp")));
GlobalConstant.i("-----------------");
}
}
}
1.建立dbhelper类:
Context context;
public static int DB_VERSION = 1;
public static String DB_NAME = "d_weather.db";
public static String TABLE_NAME = "t_weather";
/**
* 构造方法
*
* @param context
* @param name
* @param factory
* @param version
*/
public SQLiteDataHelp(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// SQLiteOpenHelper子类必须要的一个构造函数
}
/**
* 构造方法
*/
public SQLiteDataHelp(Context context) {
// 必须实现父类构造方法
this(context, DB_VERSION);
}
public SQLiteDataHelp(Context context, int version) {
// 必须实现父类构造方法
this(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 数据库初始化
db.execSQL("create table if not exists " + TABLE_NAME + "("
+ "w_id integer primary key," + "w_name varchar,"
+ "w_temp varchar)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 当打开数据库时传入的版本号与当前的版本号不同时会调用该方法
}
public void insert(ContentValues contentValues) {
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, contentValues);
db.close();
}
/**
*
* query
*
* @return
*/
public Cursor query() {
SQLiteDatabase db = getReadableDatabase();
// 获取Cursor
Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null,
null);
return c;
}
//更新数据库的内容
public void update(ContentValues values, String whereClause, String[]whereArgs){
SQLiteDatabase db = getWritableDatabase();
db.update(TABLE_NAME, values, whereClause, whereArgs);
}
// 根据唯一标识_id 来删除数据
public void delete(int id) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_NAME, "w_id=?", new String[] { String.valueOf(id) });
}
2.调用CRUD方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// CRUD操作
// insert
SQLiteDataHelp dbhelp = new SQLiteDataHelp(MainActivity.this);
ContentValues contentValues = new ContentValues();
contentValues.put("w_name", "北京");
contentValues.put("w_temp", "22°C");
// dbhelp.insert(contentValues);
// delete
// dbhelp.delete(1);
// 修改
dbhelp.update(contentValues, "w_name='北京'", null);
// 查询
Cursor cursor = dbhelp.query();
if (cursor.getCount() == 0) {
GlobalConstant.i("no data!!");
} else {
while (cursor.moveToNext()) {
GlobalConstant.i("w_id--->"
+ cursor.getInt(cursor.getColumnIndex("w_id")));
GlobalConstant.i("w_name--->"
+ cursor.getString(cursor.getColumnIndex("w_name")));
GlobalConstant.i("w_temp--->"
+ cursor.getString(cursor.getColumnIndex("w_temp")));
GlobalConstant.i("-----------------");
}
}
}
2013年8月8日星期四
mysql命令语法3
- 添加外键语法:
alter table t_user add constraint u_paid_id2p_id
foreign key (u_paid_id)
references t_paid (p_id)
on delete cascade on update cascade;
注意key后面不要写表名,如果写成这样:
alter table t_user add constraint u_paid_id2p_id
foreign key t_user(u_paid_id)
references t_paid (p_id)
on delete cascade on update cascade;
会报错:ERROR 1061 (42000): Duplicate key name 't_user'
foreign key t_user(u_paid_id)
references t_paid (p_id)
on delete cascade on update cascade;
会报错:ERROR 1061 (42000): Duplicate key name 't_user'
原因是mysql会把表名't_user'当成外键名称,然后再次新建外键时候会报重复外键的错,具体信息请参看
mysql>show innodb status;
找到LATEST FOREIGN KEY ERROR一行
- 删除外键出错
解决方法:正确删除索引后在删除key
出错原因:使用了错误的外键名称,详见show innodb status命令
2013年8月7日星期三
mysql语法命令2
清空所有字段: truncate table xf_tb
查看key: show keys from xf_bt
查看索引: show index from xf_bt
删除索引: alter table xf_tb drop index idx_us
查看建表语句: show create table xf_tb
修改表名: rename table xf_tb to xf_bt
查看是否有其他进程连接: show process list;
停止,启动mysql服务 service mysql start/stop
重命名数据库:
1.进入mysql的bin目录
2.进入mysql命令行:create database shop(新数据库名)
3.use shop
4.\. e:\shop.sql
2012年8月24日星期五
eclipse 4.2.0 juno 同步svn错误解决
右键使用svn share project出现以下错误:
org.tigris.subversion.javahl.ClientException: Unsupported working copy format
svn: This client is too old to work with working copy 'E:\android workspace\xf19_GPS_share'. You need
to get a newer Subversion client, or to downgrade this working copy.
See http://subversion.tigris.org/faq.html#working-copy-format-change
for details.
org.tigris.subversion.javahl.ClientException: Unsupported working copy format
svn: This client is too old to work with working copy 'E:\android workspace\xf19_GPS_share'. You need
to get a newer Subversion client, or to downgrade this working copy.
See http://subversion.tigris.org/faq.html#working-copy-format-change
for details.
解决办法:
1.下载最新的subversion 1.8.x,在install new software中添加最新subversion,地址为:http://subclipse.tigris.org/update_1.8.x/
2.在项目右键-team-share project,然后直接下一步完成即可
升级成功:
subversion:1.8.3
subclipse:1.8.16
订阅:
博文 (Atom)
