2017-05-06 99 views
0

我有一個功能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(); 
    } 
} 
+0

安置自己的swapCursor()代碼 –

+0

@ R.R.M新增你要求 – Mikitz06

+0

我已要求swapCursor()適配器類的方法。這是getAllItems()方法。 –

回答

0

請您Items.java增加一個行你onCreate()文件

spinnerCategory = (Spinner) findViewById(R.id.categorySpinner); 
//this is new line 
spinnerCategory.setOnItemSelectedListener(this); 

您的活動類看起來像下面

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); 
     //here set item selected listener of spinner 
     spinnerCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       String name = spinnerCategory.getSelectedItem().toString(); 
       mAdapter.swapCursor(getAllItems(name)); 
       mAdapter.notifyDataSetChanged(); 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 
    } 

    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(); 
    } 
} 
+0

這是做什麼?另外,在不使用某種適配器之前,我不能調用'notifyDataSetChanged()',就像'RecyclerView.notifyDataSetChanged();' – Mikitz06

0

我們都錯過了一些非常簡單的。我忘了將spinnerCategory.setOnItemSelectedListener(this);添加到activity.class。

/捂臉