1. 程式人生 > >Android仿QQ登錄下拉歷史列表

Android仿QQ登錄下拉歷史列表

tostring ring detail oncreate 文章 sqlite數據庫 ecs 手機 按鈕

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

Android仿QQ登錄下拉歷史列表