RecyclerViewAdapter
package com.example.roomtwo; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.lifecycle.LiveData; import androidx.recyclerview.widget.ListAdapter; import androidx.recyclerview.widget.RecyclerView; import com.example.roomtwo.dao.Word; import java.util.List; public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.InnerHolder> { List<Word> allWordLive = null; public void setAllWordLive(List<Word> allWordLive) { this.allWordLive = allWordLive; } //创建viewHolder的时候呼叫的 @NonNull @Override public RecyclerViewAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(parent.getContext(), R.layout.recyclerview_item, null); return new InnerHolder(view); } //viewHolder和recyclerview绑定时呼叫的 @Override public void onBindViewHolder(@NonNull RecyclerViewAdapter.InnerHolder holder, int position) { Word word = allWordLive.get(position); holder.textView_word.setText(String.valueOf((position+1)+": "+word.getWord())); holder.textView_meaning.setText(String.valueOf(word.getChineseMeaning())); } @Override public int getItemCount() { return allWordLive.size(); } public class InnerHolder extends RecyclerView.ViewHolder { TextView textView_word; TextView textView_meaning; public InnerHolder(@NonNull View itemView) { super(itemView); textView_word = itemView.findViewById(R.id.item_word); textView_meaning = itemView.findViewById(R.id.item_meaning); } } }RecyclerViewAdapter
MainActivity
package com.example.roomtwo; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.LiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProviders; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.room.Room; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.example.roomtwo.dao.Word; import com.example.roomtwo.dao.WordDao; import com.example.roomtwo.dao.WordDatabase; import java.util.List; public class MainActivity extends AppCompatActivity { WordViewModel wordViewModel; RecyclerViewAdapter adapter; RecyclerView myRecyclerView; Button insertBtn; Button updateBtn; Button clearBtn; Button deleteBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //viewModel的实例化 wordViewModel = ViewModelProviders.of(this).get(WordViewModel.class); //******************************recyclerview************************************************ myRecyclerView = findViewById(R.id.myrecyclerview); adapter = new RecyclerViewAdapter(); myRecyclerView.setLayoutManager(new LinearLayoutManager(this)); myRecyclerView.setAdapter(adapter); //可被观察的数据 wordViewModel.getAllWordLive().observe(this, new Observer<List<Word>>() { @Override public void onChanged(List<Word> words) { adapter.setAllWordLive(words); adapter.notifyDataSetChanged(); //告诉数据改变,刷新视图 } }); //初始化各组件 initBtnAndView(); //插入 insertBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Word word = new Word("hello", "你好"); Word word1 = new Word("world", "世界"); wordViewModel.insertWords(word,word1); } }); //清空数据库表 clearBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { wordViewModel.deleteAllWords(); } }); //删除某一个字段 deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Word word = new Word("", ""); word.setId(270); wordViewModel.deleteWords(word); } }); updateBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Word word = new Word("", ""); word.setId(269); wordViewModel.updateWords(word); } }); } void initBtnAndView(){ insertBtn = findViewById(R.id.insertBtn); updateBtn = findViewById(R.id.updateBtn); clearBtn = findViewById(R.id.clearBtn); deleteBtn = findViewById(R.id.deleteBtn); } }MainActivity