首页 > 其他分享 >团队冲刺开发第二天

团队冲刺开发第二天

时间:2022-11-29 14:38:31浏览次数:39  
标签:androidx void 冲刺 wordDao 第二天 words import 团队 public

今天花费将近3个多小时的时间,将以前的Android知识回顾了一遍,同时自己针对自己的任务做了一个小Demo。

主要用到的是ViewModel来存储页面数据,Respositry仓库来进行数据的增删改查,还有一个Database数据库,一个Dao和展示数据的Adapter

在数据的增删改查利用的事AsynceTask的副线程,创建singleton单实例数据库。

在实现的过程中,出现的问题有:

对AsynceTask的线程问题不熟悉,一开始的做法是直接在活动中强制在主线程执行增删改查,查了相关操作后才发现可以用AsynceTask的副线程,即安全又可靠。

Word.java

package com.example.roombasic;

import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity
public class Word {
@PrimaryKey(autoGenerate = true)
private int id;
private String word;
private String meaning;

public Word(String word, String meaning) {
this.word = word;
this.meaning = meaning;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getWord() {
return word;
}

public void setWord(String word) {
this.word = word;
}

public String getMeaning() {
return meaning;
}

public void setMeaning(String meaning) {
this.meaning = meaning;
}
}

View Code

WordDao.java

package com.example.roombasic;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;
import java.util.ListIterator;

@Dao
public interface WordDao {
@Insert
void insertWord(Word... words);

@Update
void updateWord(Word... words);

@Delete
void deleteWord(Word... words);

@Query("delete from word")
void deleteALLWord();

@Query("select * from word order by id desc")
//List<Word> getAllWord();
LiveData<List<Word>>getAllWordsLive();
}

View Code

WordDataBase.java

团队冲刺开发第二天_ide


package com.example.roombasic;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
@Database(entities = {Word.class},version=1,exportSchema = false)
//singleton单实例数据库
public abstract class WordDatabase extends RoomDatabase {
private static WordDatabase INSTANCE;
static synchronized WordDatabase getDatebase(Context context){
if(INSTANCE==null){
INSTANCE= Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database")
.build();
}
return INSTANCE;
}
public abstract WordDao getWordDao();
}

View Code

WordRepository.java(仓库类)

团队冲刺开发第二天_ide


package com.example.roombasic;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;
//仓库类,用来管理viewmodel的增删改查操作
class WordRepository {
private LiveData<List<Word>> allWordsLive;
private WordDao wordDao;

WordRepository(Context context) {
WordDatabase wordDatabase = WordDatabase.getDatebase(context.getApplicationContext());
wordDao = wordDatabase.getWordDao();
allWordsLive = wordDao.getAllWordsLive();
}

LiveData<List<Word>> getAllWordsLive() {
return allWordsLive;
}

void insertWords(Word... words) {
new InsertAsynceTask(wordDao).execute(words);
}

void updateWords(Word... words) {
new UpdateAsynceTask(wordDao).execute(words);
}

void deleteWords(Word... words) {
new DeleteAsynceTask(wordDao).execute(words);
}

void deleteallWords() {
new DeleteAllAsynceTask(wordDao).execute();
}

static class InsertAsynceTask extends AsyncTask<Word, Void, Void> {
private WordDao wordDao;

InsertAsynceTask(WordDao wordDao) {
this.wordDao = wordDao;
}

@Override
protected Void doInBackground(Word... words) {
wordDao.insertWord(words);
return null;
}
}

static class UpdateAsynceTask extends AsyncTask<Word, Void, Void> {
private WordDao wordDao;

UpdateAsynceTask(WordDao wordDao) {
this.wordDao = wordDao;
}

@Override
protected Void doInBackground(Word... words) {
wordDao.updateWord(words);
return null;
}
}

static class DeleteAsynceTask extends AsyncTask<Word, Void, Void> {
private WordDao wordDao;

DeleteAsynceTask(WordDao wordDao) {
this.wordDao = wordDao;
}

@Override
protected Void doInBackground(Word... words) {
wordDao.deleteWord(words);
return null;
}
}

static class DeleteAllAsynceTask extends AsyncTask<Void, Void, Void> {
private WordDao wordDao;

DeleteAllAsynceTask(WordDao wordDao) {
this.wordDao = wordDao;
}

@Override
protected Void doInBackground(Void... voids) {
wordDao.deleteALLWord();
return null;
}
}
}

View Code

WordViewModel(页面数据)

团队冲刺开发第二天_ide


package com.example.roombasic;

import android.app.Application;
import android.os.AsyncTask;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import java.util.List;

public class WordViewModel extends AndroidViewModel {
private WordDao wordDao;
private WordRepository wordRepository;
private LiveData<List<Word>> allWordsLive;
public WordViewModel(@NonNull Application application) {
super(application);
wordRepository=new WordRepository(application);
}

LiveData<List<Word>> getAllWordsLive() {
return wordRepository.getAllWordsLive();
}
void insertWords(Word...words){
wordRepository.insertWords(words);
}
void updateWords(Word...words){
wordRepository.updateWords(words);
}
void deleteWords(Word...words){
wordRepository.deleteWords(words);
}
void deleteallWords(){
wordRepository.deleteallWords();
}


}

View Code

MyAdapter(适配器)

package com.example.roombasic;

import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHoder> {
List<Word> allwords=new ArrayList<>();
Boolean aBoolean;

public MyAdapter(Boolean aBoolean) {
this.aBoolean = aBoolean;
}

public void setAllwords(List<Word> allwords) {
this.allwords = allwords;
}

@NonNull
@Override
public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View item;
if(aBoolean){
item =layoutInflater.inflate(R.layout.cell_card,parent,false);
}else{
item =layoutInflater.inflate(R.layout.cell_normal,parent,false);
}
return new MyViewHoder(item);
//return null;
}

@Override
public void onBindViewHolder(@NonNull final MyViewHoder holder, int position) {
Word word=allwords.get(position);
holder.textViewNumber.setText(String.valueOf(position+1));
holder.textViewEnglish.setText(word.getWord());
holder.textViewChinese.setText(word.getMeaning());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri=Uri.parse("http://m.youdao.com/dict?le=eng&q="+holder.textViewEnglish.getText());
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
holder.itemView.getContext().startActivity(intent);
}
});
}

@Override
public int getItemCount() {
return allwords.size();
}

static class MyViewHoder extends RecyclerView.ViewHolder{
TextView textViewNumber,textViewEnglish,textViewChinese;
public MyViewHoder(@NonNull View itemView) {
super(itemView);
textViewNumber=itemView.findViewById(R.id.textViewNumber);
textViewChinese=itemView.findViewById(R.id.textViewChinese);
textViewEnglish=itemView.findViewById(R.id.textViewEnglish);

}
}

}

View Code

MainActivity

团队冲刺开发第二天_ide


package com.example.roombasic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
WordDatabase wordDatabase;
WordDao wordDao;
Button insertbutton,clearbutton;
LiveData<List<Word>>allWordsLive;
WordViewModel wordViewModel;
RecyclerView recyclerView;
Switch aSwitch;
MyAdapter myAdapter1,myAdapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wordViewModel= ViewModelProviders.of(this).get(WordViewModel.class);
recyclerView=findViewById(R.id.recycleview);
myAdapter1=new MyAdapter(false);
myAdapter2=new MyAdapter(true);
//加入线性布局
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//默认第一个是适配器为正常格式
recyclerView.setAdapter(myAdapter1);
//控件切换展示的格式
aSwitch=findViewById(R.id.switchButton);

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
recyclerView.setAdapter(myAdapter2);
}else{
recyclerView.setAdapter(myAdapter1);
}
}
});
//监视数据的变化,及时更新
wordViewModel.getAllWordsLive().observe(this, new Observer<List<Word>>() {
@Override
public void onChanged(List<Word> words) {
myAdapter1.setAllwords(words);
myAdapter2.setAllwords(words);
myAdapter1.notifyDataSetChanged();
myAdapter2.notifyDataSetChanged();
}
});

insertbutton=findViewById(R.id.addbutton);

clearbutton=findViewById(R.id.clearbutton);

insertbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String []english={
"hello",
"world",
"apple",
"banana",
"pear",
"watermelon"
};
String []chinese={
"你好",
"世界",
"苹果",
"香蕉",
"梨",
"西瓜"
};
//副线程插入
//new InsertAsynceTask(wordDao).execute(w1,w2);
for(int i=0;i<english.length;i++)
wordViewModel.insertWords(new Word(english[i],chinese[i]));
}
});

clearbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wordViewModel.deleteallWords();

}
});


}


}

View Code

页面实现:

卡片布局

团队冲刺开发第二天_android_05

 

 

点击视图切换后,跟换为正常布局:

团队冲刺开发第二天_java_06

 

 点击相应的item,会跳转到有道词典来翻译当前的单词:

例如点击watermelon这个单词后:

就会跳转掉到:

团队冲刺开发第二天_java_07

 

 

-------------------------------------------------------------------------------------------------------------------------------------------------

明天要做的是:利用今天复习的东西,先实现基础的整体轮廓的布局,再将python爬取的新闻信息做成卡片展示

 

 



标签:androidx,void,冲刺,wordDao,第二天,words,import,团队,public
From: https://blog.51cto.com/u_14824425/5894962

相关文章

  • vue3响应式原理以及ref和reactive区别还有vue2/3生命周期的对比,第二天
    前言:前天我们学了ref和reactive,提到了响应式数据和Proxy,那我们今天就来了解一下,vue3的响应式在了解之前,先复习一下之前vue2的响应式原理vue2的响应式:原理:对......
  • vue3 第二天vue响应式原理以及ref和reactive区别
    前言:前天我们学了ref和reactive,提到了响应式数据和Proxy,那我们今天就来了解一下,vue3的响应式在了解之前,先复习一下之前vue2的响应式原理vue2的响应式:原理:对......
  • .NET团队送给.NET开发人员的云原生学习资源
    企业正在迅速采用云的功能来满足用户需求,提高应用程序的可伸缩性和可用性。要完全拥抱云并优化节约成本,就需要在设计应用程序时考虑到云的环境,也就是要用云原生的应用开发方......
  • 首发:徐亦达团队新论文推荐(ECCV2020):端到端多人多视角3d动态匹配网络
    徐亦达团队在ECCV2020上发表了一篇机器学习论文End-to-endDynamicMatchingNetworkforMulti-viewMulti-person3dPoseEstimation(端到端多人多视角3d动态匹配网络)论文......
  • 【首发】徐亦达团队新论文推荐:模限界矩阵分解
    徐亦达团队在IEEETransactionsonKnowledgeandDataEngineering的发表了一篇机器学习论文MagnitudeBoundedMatrixFactorisationforRecommenderSystems(模限界矩阵......
  • 从康威定律看团队架构
    康威定律是马尔文·康威1967提出的:“设计系统的架构受制于产生这些设计的组织的沟通结构。”中文直译的意思是:设计系统的组织,其产生的设计等同于组织之内、组织之间的沟通......
  • [D0g3] 第五届安洵杯wp - HashRun安全团队
    第五届安洵杯WriteUp-HashRun安全团队前言HashRun安全团队最终排名56。Crypto@S1gMa知识点\(sha256\)掩码爆破。解题过程\(step1\):先\(nc\)链接获取密文,然后......
  • 用例技术(四)——用例编写工作过程和大型团队中用例收集的建议
     一.实际编写用例,每个开发组都应该形成并制定一套工作习惯,一种好的方式是由开发组和个人分工合作,因为组比个人有两个优点,一是便于集中讨论,二是便于对于问题达成共......
  • 带领团队
    团队一个有口才的人对着一群有耳朵的人讲话,这就是最基本的团队。 言归正传。将带领团队的一些想法记录下来,与君探讨。长夜漫漫,我有故事,你有酒。一、如何提升项目交付......
  • HDLbits第二天
    出于对FPGA学习巩固的目的,同时也希望能锻炼自己对于Verilog的题目分析,让自己对HDL代码的理解加深,所以想坚持写一下关于HDLbits网站刷题的系列,计划是工作日每日5题目+分析,周......