我正在尝试创建字典应用程序,但是当我运行该应用程序运行时,却收到“复制失败”吐司,并且在日志中,我发现此错误“ E / RecyclerView:未连接适配器;跳过布局” 。请在我的代码下方找到此处,并提供屏幕截图。
我已经放置了SQLite file in the assets folder
。
您能帮我解决这个问题吗?
最好的问候, 拉明
这是我的Word_Adapter类代码
package com.example.androiddictionarywithsqlite.adapters;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.androiddictionarywithsqlite.DictionaryModel;
import com.example.androiddictionarywithsqlite.R;
import java.util.List;
public class Word_Adapter extends RecyclerView.Adapter<Word_Adapter.ViewHolder> {
public List<DictionaryModel> data;
public Word_Adapter(){}
public void setData(List<DictionaryModel>data) {
this.data = data;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View wordView = inflater.inflate(R.layout.word_item,viewGroup,false);
ViewHolder viewHolder = new ViewHolder(wordView,context);
return viewHolder;
}
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
DictionaryModel dictionaryModel = data.get(position);
viewHolder.wordText.setText(dictionaryModel.getWord());
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public Context context;
public TextView wordText;
public ViewHolder(View itemView, final Context context) {
super(itemView);
this.context = context;
wordText = (TextView) itemView.findViewById(R.id.wordtext);
}
}
}
这是我的MainActivity类代码
public class MainActivity extends AppCompatActivity {
private RecyclerView rvWord;
private Word_Adapter word_adapter;
private List<DictionaryModel> dictionaryModelList;
private DatabaseHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvWord = (RecyclerView) findViewById(R.id.rvWord);
rvWord.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mDBHelper = new DatabaseHelper(this);
File database=getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
if (database.exists()== false){
mDBHelper.getReadableDatabase();
if(copyDatabase(this)){
Toast.makeText(getApplicationContext(),"copy successful",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(),"copy failed",Toast.LENGTH_LONG).show();
return;
}
}
dictionaryModelList=mDBHelper.getlistWord("");
word_adapter = new Word_Adapter();
word_adapter.setData(dictionaryModelList);
rvWord.setAdapter(word_adapter);
}
private boolean copyDatabase(Context context) {
try{
InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);
String outFileName=DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[]buff= new byte[1024];
int length = 0;
while ((length=inputStream.read(buff))>0){
outputStream.write(buff,0,0);
}
outputStream.flush();
outputStream.close();
Log.w("Database","copy successfully");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
这是我的DatabaseHelper calss代码
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydictionary.sqlite";
public static final String DBLOCATION = "/data/data/com.example.androiddictionarywithsqlite/database/";
private Context mcontext;
private SQLiteDatabase mDatabase;
public DatabaseHelper(Context context){
super(context,DBNAME,null,1);
this.mcontext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDatabase(){
String dbPath=mcontext.getDatabasePath(DBNAME).getPath();
if (mcontext!=null && mDatabase.isOpen()){
return;
}
mDatabase=SQLiteDatabase.openDatabase(dbPath,null,SQLiteDatabase.OPEN_READWRITE);
}
public void closeDatabase(){
if(mDatabase!=null){
mDatabase.close();
}
}
public List<DictionaryModel> getlistWord(String wordSearch){
DictionaryModel dictionaryModel = null;
List<DictionaryModel> dictionaryModelList = new ArrayList<>();
openDatabase();
String[] args={"%"+wordSearch+"%"};
Cursor cursor= mDatabase.rawQuery("Select * From tblWord Where word Like ?",args);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
dictionaryModel = new DictionaryModel(cursor.getString(0),cursor.getString(1),cursor.getString(2));
dictionaryModelList.add(dictionaryModel);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return dictionaryModelList;
}
}
这是我的DictionaryModel类的代码
package com.example.androiddictionarywithsqlite;
public class DictionaryModel {
private String id;
private String word;
private String definition;
public DictionaryModel(String id, String word, String definition) {
this.id = id;
this.word = word;
this.definition = definition;
}
public String getId() {
return id;
}
public String getWord() {
return word;
}
public String getDefinition() {
return definition;
}
public void setId(String id) {
this.id = id;
}
public void setWord(String word) {
this.word = word;
}
public void setDefinition(String definition) {
this.definition = definition;
}
}
这是activity_main的Xlm代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:iconifiedByDefault="false"
app:queryHint="Search"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvWord"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/searchView"
/>
</RelativeLayout>
这是word_item的xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="@android:color/transparent"
android:foreground="?android:attr/selectableItemBackground"
>
<TextView
android:id="@+id/wordText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
/>
</LinearLayout>