根据Spinner输入动态填充RecyclerView

时间:2017-05-06 04:58:46

标签: android android-recyclerview spinner

我有一个已填充RecyclerView的功能onCreate,但是当更改微调器的选择时,RecyclerView将不会更新。 RecyclerView功能中的其他所有内容都完美无瑕。即使我手动编辑Spinner的选择,它也会更改onCreate,但仅 onCreate

有趣的是,当点击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();
    }
}

1 个答案:

答案 0 :(得分: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();
    }
}