gpt4 book ai didi

Android Studio 将数据库填充到 RecyclerVIew

转载 作者:搜寻专家 更新时间:2023-10-30 22:34:26 26 4
gpt4 key购买 nike

我正在尝试将数据库填充到 RecyclerVIew,在我拥有 ListView 之前一切正常,现在当我运行我的应用程序时它什么都不显示。

这是我第一次使用 recyclerview,所以我对整个概念还是陌生的,我认为 listview 和 recyclerview 只是在 View 上没有什么不同,无论如何我被困住了,我很感激一些帮助

这是我的类(class)

主 Activity

public class MainActivity extends AppCompatActivity {
private ArrayList<Word> mwordlist;
private WordAdapter adapter;
private RecyclerView recyclerView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

adapter = new WordAdapter(getApplicationContext(),mwordlist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());


recyclerView.setAdapter(adapter);

DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);

databaseAccess.open();

mwordlist = databaseAccess.getWords(mwordlist);


adapter.notifyDataSetChanged();

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

WordAdapter

public class WordAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context context;
ArrayList<Word> mWordlist;

public WordAdapter(Context context, ArrayList<Word> mWordlist) {
this.context = context;
this.mWordlist = mWordlist;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.word_listview,null);
MyViewHolder holder = new MyViewHolder(view);

return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

holder.tword.setText(mWordlist.get(position).getmWord());
holder.tkalmad.setText(mWordlist.get(position).getmKalmad());
}

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

数据库访问

public class DatabaseAccess {

private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;

/**
* Private constructor to avoid object creation from outside classes.
*
* @param context
*/
private DatabaseAccess(Context context) {
this.openHelper = new DatabaseOpenHelper(context);
}

/**
* Return a singleton instance of DatabaseAccess.
*
* @param context the Context
* @return the instance of DabaseAccess
*/
public static DatabaseAccess getInstance(Context context) {
if (instance == null) {
instance = new DatabaseAccess(context);
}
return instance;
}

/**
* Open the database connection.
*/
public void open() {
this.database = openHelper.getWritableDatabase();
}

/**
* Close the database connection.
*/
public void close() {
if (database != null) {
this.database.close();
}
}


public ArrayList<Word> getWords(ArrayList<Word> WordList) {
Word word = null;
Cursor cursor = database.rawQuery("SELECT * FROM dictionary", null);
cursor.moveToFirst();
WordList.clear();
while (!cursor.isAfterLast()) {
word = new Word(cursor.getString(1),cursor.getString(2));
WordList.add(word);
cursor.moveToNext();
}
cursor.close();

return WordList;
}
}

....我尝试了很多解决方案,但我做不对

新的 logcat

08-21 22:42:47.903 20536-20536/com.example.zer0_ll.somalimeddictionary  D/ContextImpl: ContextImpl running for user UserHandle{0} 0
08-21 22:42:47.903 20536-20536/com.example.zer0_ll.somalimeddictionary W/ResourcesManager: getTopLevelResources: null for user 0
08-21 22:42:47.913 20536-20536/com.example.zer0_ll.somalimeddictionary W/ResourcesManager: getTopLevelResources: null for user 0
08-21 22:42:47.913 20536-20536/com.example.zer0_ll.somalimeddictionary D/DisplayManager: DisplayManager()
08-21 22:42:48.294 20536-20543/com.example.zer0_ll.somalimeddictionary W/art: Suspending all threads took: 43.579ms
08-21 22:42:48.304 20536-20536/com.example.zer0_ll.somalimeddictionary D/PhoneWindow: *FMB* installDecor mIsFloating : false
08-21 22:42:48.304 20536-20536/com.example.zer0_ll.somalimeddictionary D/PhoneWindow: *FMB* installDecor flags : -2139029248
08-21 22:42:48.434 20536-20536/com.example.zer0_ll.somalimeddictionary W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(andro id.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-21 22:42:48.764 20536-20543/com.example.zer0_ll.somalimeddictionary W/art: Suspending all threads took: 11.779ms
08-21 22:42:48.924 20536-20536/com.example.zer0_ll.somalimeddictionary I/SQLiteAssetHelper: successfully opened database Database.db
08-21 22:42:48.924 20536-20536/com.example.zer0_ll.somalimeddictionary D/AndroidRuntime: Shutting down VM
08-21 22:42:48.924 20536-20536/com.example.zer0_ll.somalimeddictionary E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zer0_ll.somalimeddictionary, PID: 20536
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zer0_ll.somalimeddictionary/com.example.zer0_ll.somalimeddictionary.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.ArrayList.clear()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.ArrayList.clear()' on a null object reference
at com.example.zer0_ll.somalimeddictionary.DatabaseAccess.getWords(DatabaseAccess.java:62)
at com.example.zer0_ll.somalimeddictionary.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6178)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

最佳答案

getWords()内部功能DatabaseAccess您正在创建新的 Arraylist ( ArrayList<Word> WordList = new ArrayList<>();) 并返回此 Arraylist 的类,因此此函数返回具有新内存位置的 ArrayList 但您的适配器取决于 mWordlist这是在 MainActivity这就是为什么 adapter.notifyDataSetChanged();不工作。尝试发送引用 mWordlist在 MainActivity 到 getWords()功能类似于getWords(mWordlist)getWords()使用这个 ArrayList。不要分配新的 ArrayList。您可以在向 ArrayList 中添加新数据之前清除此 ArrayList。

在 getWords() 函数中删除它

 ArrayList<Word> WordList = new ArrayList<>();

编辑:

getWords()函数代码:

public ArrayList<Word> getWords(ArrayList<Word> WordList) {
Word word = null;
Cursor cursor = database.rawQuery("SELECT * FROM dictionary", null);
cursor.moveToFirst();
WordList.clear()
while (!cursor.isAfterLast()) {
word = new Word(cursor.getString(1),cursor.getString(2));
WordList.add(word);
cursor.moveToNext();
}
cursor.close();

return WordList;
}

主要 Activity 代码

public class MainActivity extends AppCompatActivity {
private ArrayList<Word> mWordlist;
private WordAdapter adapter;
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

mWordlist = new ArrayList<>()
adapter = new WordAdapter(getApplicationContext(),mWordlist);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());


recyclerView.setAdapter(adapter);

DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);

databaseAccess.open();

mWordlist = databaseAccess.getWords(mWordlist);


adapter.notifyDataSetChanged();

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

关于Android Studio 将数据库填充到 RecyclerVIew,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com