点击注册键,会把用户名存储到数据库中;点击下拉图标,显示出所有存储在数据库中的用户名。
public class MainActivity extends Activity implements OnClickListener{
private PopupWindow pop;
private EditText username_et;
private ImageView drop_down_iv;
private Button register_btn;
private ListView listView;
private List list = new ArrayList();
private UserDao userDao;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
userDao = new UserDao(this);
}
private void initView() {
username_et = (EditText) findViewById(R.id.username_et);
drop_down_iv = (ImageView) findViewById(R.id.username_drop_down_iv);
register_btn = (Button) findViewById(R.id.register_btn);
username_et.setOnClickListener(this);
drop_down_iv.setOnClickListener(this);
register_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.register_btn:
String name = username_et.getText().toString();
userDao.insert(name, null);
username_et.setText("");
Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
break;
case R.id.username_drop_down_iv:
getPopWindow();
pop.showAsDropDown(v,100,0);
break;
}
}
private void getPopWindow() {
if (pop != null){
pop.dismiss();
return;
}else {
initPopWindow();
}
}
private void initPopWindow() {
View contentView = View.inflate(this,R.layout.pop_contentview,null);
listView = (ListView) contentView.findViewById(R.id.username_lv);
list = userDao.query();
ArrayAdapter<User> adapter = new ArrayAdapter<User>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
User user = list.get(position);
String name = user.getName();
username_et.setText(name);
}
});
pop = new PopupWindow(contentView,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,true);
//触摸事件,对显示出来的东西进行监听
contentView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (pop != null && pop.isShowing()) {
pop.dismiss();
pop = null;
}
return false;
}
});
}
}
用于处理数据库的Dao层
public class UserDao {
private MyOpendhelper myOpendhelper;
private SQLiteDatabase db;
private static final String TABLE_NAME = “user”;
private Context context;
public UserDao(Context context){
myOpendhelper = new MyOpendhelper(context);
this.context = context;
}
public void insert(String userName,String password){
db = myOpendhelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("userName", userName);
values.put("password", password);
db.insert(TABLE_NAME, null, values);
Toast.makeText(context,"插入成功",Toast.LENGTH_SHORT).show();
db.close();
}
public List<User> query(){
List<User> list = new ArrayList<User>();
db = myOpendhelper.getWritableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null,null);
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
String pwd = cursor.getString(2);
User user = new User();
user.setName(name);
user.setPassword(pwd);
user.setId(id);
list.add(user);
}
return list;
}
}
继承SQLiteOpenHelper的工具类
public class MyOpendhelper extends SQLiteOpenHelper{
public MyOpendhelper(Context context) {
super(context,"info.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createsql = "create table user(_id integer primary key autoincrement,username,password)";
db.execSQL(createsql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}