Android仿QQ登录下拉历史列表

最近项目开发中做到注册登录时,产品要求登录历史列表类似于QQ登录,经过一番的思考着手开始撸代码,下面是两张效果图。下载完整demo地址:https://download.csdn.net/download/lou_liang/10418605


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

首先创建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) {

    }

接着创建存储历史的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;
    }
}
然后监听是否点击登录历史按钮,如果上次登录成功,那么将这条数据插入到数据库中,点击历史按钮时查询列表,并且按照登录时间降序。

//是否显示历史登录列表
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/lou_liang/10418605


  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值