我有一個功能RecyclerView
填充onCreate
,但是當更改微調器的選擇時,RecyclerView
不會更新。 RecyclerView
功能的其他內容完美無瑕。即使我手動編輯Spinner的選擇,它也會更改onCreate
,但只有onCreate
。基於微調器輸入動態填充RecyclerView
有趣的是,當點擊upNavigation
箭頭時或後退按鈕被點擊時,基於當前選擇RecyclerView
更新。
如果您需要了信息,請不要猶豫,問:)
這裏的onItemSelected
:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.categorySpinner:
String name = spinnerCategory.getSelectedItem().toString();
mAdapter.swapCursor(getAllItems(name));
mAdapter.notifyDataSetChanged();
break;
}
}
ItemAdapter
類:
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemHolder> {
private Context mContext;
private Cursor mCursor;
public ItemAdapter(Context context, Cursor cursor) {
this.mContext = context;
this.mCursor = cursor;
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.list_item_items, parent, false);
return new ItemHolder(view);
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
if (!mCursor.moveToPosition(position))
return;
String name = mCursor.getString(
mCursor.getColumnIndex(ItemContract.ItemEntry.COLUMN_ITEM_NAME));
long id = mCursor.getLong(
mCursor.getColumnIndex(ItemContract.ItemEntry._ID));
holder.itemName.setText(name);
holder.itemView.setTag(id);
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor(Cursor newCursor) {
if (mCursor != null) mCursor.close();
mCursor = newCursor;
if (newCursor != null) {
this.notifyDataSetChanged();
}
}
class ItemHolder extends RecyclerView.ViewHolder {
TextView itemName;
public ItemHolder(View itemView) {
super(itemView);
itemName = (TextView) itemView.findViewById(R.id.itemName);
}
}
}
這裏的Activity.class
public class Items extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private ItemAdapter mAdapter;
SQLiteDatabase mDb;
Spinner spinnerCategory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
DBHelper DBHelper = new DBHelper(this);
RecyclerView itemsRecylerView;
itemsRecylerView = (RecyclerView) this.findViewById(R.id.items_recylerview);
itemsRecylerView.setLayoutManager(new LinearLayoutManager(this));
spinnerCategory = (Spinner) findViewById(R.id.categorySpinner);
List<String> categories = DBHelper.getCategoryList();
ArrayAdapter<String> aCategory = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, categories);
aCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(aCategory);
mDb = DBHelper.getReadableDatabase();
String name = spinnerCategory.getSelectedItem().toString();
Cursor cursor = getAllItems(name);
mAdapter = new ItemAdapter(this, cursor);
itemsRecylerView.setAdapter(mAdapter);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
long id = (long) viewHolder.itemView.getTag();
removeTransaction(id);
String name = spinnerCategory.getSelectedItem().toString();
mAdapter.swapCursor(getAllItems(name));
}
}).attachToRecyclerView(itemsRecylerView);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.categorySpinner:
String name = spinnerCategory.getSelectedItem().toString();
mAdapter.swapCursor(getAllItems(name));
mAdapter.notifyDataSetChanged();
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
private Cursor getAllItems(String catName) {
return mDb.query(
ItemContract.ItemEntry.TABLE_NAME,
null,
ItemContract.ItemEntry.COLUMN_ITEM_CATEGORY + " IS " + "'" + catName + "'",
null,
null,
null,
ItemContract.ItemEntry.COLUMN_ITEM_NAME
);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean removeTransaction(long id) {
return mDb.delete(ItemContract.ItemEntry.TABLE_NAME,
ItemContract.ItemEntry._ID + "=" + id,
null) > 0;
}
public void onResume() {
super.onResume();
String name = spinnerCategory.getSelectedItem().toString();
mAdapter.swapCursor(getAllItems(name));
mAdapter.notifyDataSetChanged();
}
public void onPause() {
super.onPause();
String name = spinnerCategory.getSelectedItem().toString();
mAdapter.swapCursor(getAllItems(name));
mAdapter.notifyDataSetChanged();
}
}
安置自己的swapCursor()代碼 –
@ R.R.M新增你要求 – Mikitz06
我已要求swapCursor()適配器類的方法。這是getAllItems()方法。 –