Android仿QQ登录下拉历史列表

demo中包含了Sqlite数据库增删改查,对存储的账号进行按照最新的时间排序,限制了最多存储5条数据。
效果图:

1.首先创建MyHelper建表:

public class MyHelper extends SQLiteOpenHelper {

    public MyHelper(Context context) {
        super(context,"hayden.db",null,3);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,phone VARCHAR(20),name VARCHAR(20),time INTEGER(100),fullName VARCHAR(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

2.接着创建存储历史的bean类,包含phone,name,time这三个字段,然后创建AccountDao对数据增删改查:

public class AccountDao {
public final static String TABLE_NAME = "account";
private MyHelper helper;
private String phone;

public AccountDao(Context context){
    helper=new MyHelper(context);
}

public void insert(HistoryInfo info){
    SQLiteDatabase db=helper.getWritableDatabase();
    //根据手机号判断去重
    String[] colum = {"phone"};
    String where = "phone" + "= ?";
    String[] whereValue = {info.getPhone()};
    Cursor cursor = db.query(TABLE_NAME, colum, where, whereValue, null, null, null);
    while (cursor.moveToNext()){
        phone = cursor.getString(cursor.getColumnIndex("phone"));
    }
    cursor.close();
    ContentValues values=new ContentValues();
    values.put("phone",info.getPhone());
    values.put("name",info.getName());
    values.put("time",info.getTime());
    if(!TextUtils.isEmpty(phone)){
        db.update(TABLE_NAME,values,"phone" + "=?",new String[]{phone});
    }else {
        db.insert(TABLE_NAME,null,values);
    }
    db.close();
}
public int delete(String phone){
    SQLiteDatabase db=helper.getWritableDatabase();
    int count=db.delete(TABLE_NAME,"phone=?",new String[]{phone +""});
    db.close();
    return count;
}
public List<HistoryInfo> queryAll(){
    SQLiteDatabase db=helper.getWritableDatabase();
    Cursor cursor=db.query(TABLE_NAME,null,null,null,null,null,null);
    List<HistoryInfo> list=new ArrayList();
        while (cursor.moveToNext()) {
            HistoryInfo historyInfo = new HistoryInfo();
            historyInfo.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
            historyInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
            historyInfo.setTime(cursor.getLong(cursor.getColumnIndex("time")));
            list.add(historyInfo);
        }
        db.close();
        cursor.close();
        return list;
}

}

3.然后监听是否点击登录历史按钮,如果上次登录成功,那么将这条数据插入到数据库中,点击历史按钮时查询列表,并且按照登录时间降序。

//是否显示历史登录列表
historyCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            initPopuWindow();//显示历史列表
            if (historyList.size() == 0) {
                pwdBottom.setVisibility(View.VISIBLE);
            } else {
                pwdBottom.setVisibility(View.GONE);
            }
        } else {
            selectPopupWindow.dismiss(); //隐藏列表
            pwdBottom.setVisibility(View.VISIBLE);
        }
    }
});
@Override
public void onClick(View v) {
  switch (v.getId()){
      case R.id.loginBtn:
          if(TextUtils.isEmpty(userET.getText().toString()) || TextUtils.isEmpty(pwdET.getText().toString())){
              Toast.makeText(LoginActivity.this,"账号或者密码不能为空",Toast.LENGTH_LONG).show();
              return;
          }else {
              HistoryInfo historyInfo = new HistoryInfo(userET.getText().toString(), "Tom", new Date().getTime());
              accountDao.insert(historyInfo);
              startActivity(new Intent(LoginActivity.this,new SecondActivity().getClass()));
          }
          break;
  }
}

这样仿QQ登录历史列表就完成了,希望对看到文章的同学有所帮助。下载完整demo地址:
https://download.csdn.net/download/heishuai123/10907691

原文地址:https://blog.csdn.net/lou_liang/article/details/80339313

猜你喜欢

转载自www.cnblogs.com/wzqnxd/p/10247905.html