我有回收器的问题,问题是:我有AsyncTask从我的数据库查询数据,然后通过CursorLoader更新回收器,但适配器没有填充回收器,Logs说来自db的数据成功读取和然后,适配器构造函数成功地将ArrayList与数据一起使用,但就是这样,日志说适配器的方法不会调用。
MainActivity :
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private SQLiteDatabase db;
private SimpleCursorLoader loader;
private MySimpleAdapter adapter;
private RecyclerView recycler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler = (RecyclerView) findViewById(R.id.recycler);
new SimpleTask(SimpleTask.OPEN, null).execute();
}
private class SimpleTask extends AsyncTask<Void, Void, Void> {
public static final int INSERT = 0;
public static final int DELETE = 1;
public static final int OPEN = 2;
private int task;
private String name;
public SimpleTask(int task, String name) {
this.task = task;
this.name = name;
}
@Override
protected Void doInBackground(Void... params) {
switch (task) {
case INSERT :
ContentValues values = new ContentValues();
values.put(SimpleHelper.KEY_NAME, name);
db.insert(SimpleHelper.TABLE_NAME, null, values);
break;
case DELETE :
db.delete(SimpleHelper.TABLE_NAME, SimpleHelper.KEY_NAME + " = ? ", new String[]{name});
break;
case OPEN :
if (db == null) {
try {
db = new SimpleHelper(MainActivity.this).getWritableDatabase();
} catch (SQLiteException e) {
e.printStackTrace();
}
}
break;
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (loader == null) {
loader = new SimpleCursorLoader(MainActivity.this, db);
loader.setQueryParams(SimpleHelper.TABLE_NAME, new String[]{SimpleHelper.KEY_NAME},
null, null);
getSupportLoaderManager().initLoader(0, null, MainActivity.this);
}
getSupportLoaderManager().getLoader(0).forceLoad();
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.i("MyLog", "onLoadFinished cursor size is " + data.getCount());
List<String> items = new ArrayList<>();
data.moveToFirst();
do {
items.add(data.getString(0));
Log.i("MyLog", data.getString(0));
} while (data.moveToNext());
Log.i("MyLog", "onLoadFinished items size is " + items.size());
if (adapter == null) {
adapter = new MySimpleAdapter(items);
recycler.setAdapter(adapter);
} else {
adapter.updateAdapter(items);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
private class MySimpleAdapter extends RecyclerView.Adapter<MySimpleAdapter.MyHolder> {
private List<String> items;
public MySimpleAdapter(List<String> items) {
Log.i("MyLog", "Constructor called with items size " + items.size());
this.items = items;
}
public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvText;
public MyHolder(View itemView) {
super(itemView);
tvText = (TextView) itemView.findViewById(android.R.id.text1);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
new SimpleTask(SimpleTask.DELETE, tvText.getText().toString());
}
}
public void updateAdapter(List<String> items) {
this.items = items;
notifyDataSetChanged();
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.i("MyLog", "onCreateViewHolder");
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, null, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.tvText.setText(items.get(position));
Log.i("MyLog", "onBindViewHolder setText : " + items.get(position));
}
@Override
public int getItemCount() {
Log.d("MyLog", "getItemCount called with size : " + items.size());
return items.size();
}
}
SimpleCursorLoader:
public class SimpleCursorLoader extends CursorLoader {
private SQLiteDatabase db;
private String tableName;
private String[] columns;
public SimpleCursorLoader(Context context, SQLiteDatabase db) {
super(context);
this.db = db;
}
public void setQueryParams(String tableName, String[] columns, String selection, String[] selectionArgs) {
this.tableName = tableName;
this.columns = columns;
setSelection(selection);
setSelectionArgs(selectionArgs);
}
@Override
public Cursor loadInBackground() {
Cursor cursor = db.query(tableName, columns, getSelection(), getSelectionArgs(),
null, null, null);
return cursor;
}
DBHelper :
public class SimpleHelper extends SQLiteOpenHelper{
public static final String TABLE_NAME = "users";
public static final String KEY_NAME = "name";
public static final String KEY_ID = "_id";
private static final int DB_VERSION = 1;
private static final String DB_NAME = "SimpleDB";
public SimpleHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_NAME + " text)");
for (int i = 0; i < 5; i ++) {
ContentValues values = new ContentValues();
values.put(KEY_NAME, "name" + i+1);
db.insert(TABLE_NAME, null, values);
Log.i("MyLog", "inserted");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
答案 0 :(得分:0)
使用此功能。
recycler.setLayoutManager(new LinearLayoutManager(this));
recycler.setHasFixedSize(true);
recycler.setNestedScrollingEnabled(false);