使用适配器仅在RecyclerView中显示所选项目

时间:2016-05-08 16:35:07

标签: android android-recyclerview adapter

我有一个recyclerView来显示所有录制的音频文件。包含fileName,fileLength等音频文件的所有信息都放入cardView,然后使用适配器显示在屏幕上。

我的问题是现在显示所有录制的音频文件。我该怎么做只显示一些特定的音频文件?例如,现在我已经记录了所有音频文件,但我只想显示名称为“hello”的音频文件并隐藏其他文件。我该怎么做到这一点?有人告诉我,我可以添加一个ArrayList来过滤录制的音频,但我不知道或者我必须在哪个类中进行。非常感谢有人可以提供帮助。

我有一个RecordingItem.class来存储音频细节:

public class RecordingItem implements Parcelable {
  private String mName; 
  private String mFilePath; 
  private int mId; 
  private int mLength; 
  private long mTime; 
                       ...
  }

适配器调用如下:

    RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);

    //newest to oldest order (database stores from oldest to newest)
    llm.setReverseLayout(true);
    llm.setStackFromEnd(true);

    mRecyclerView.setLayoutManager(llm);
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());

    mFileViewerAdapter = new FileViewerAdapter(this, llm);
    mRecyclerView.setAdapter(mFileViewerAdapter);

这是适配器类:

public class FileViewerAdapter extends RecyclerView.Adapter<FileViewerAdapter.RecordingsViewHolder>
    implements OnDatabaseChangedListener {

private static final String LOG_TAG = "FileViewerAdapter";

private DBHelper mDatabase;

RecordingItem item;
Context mContext;
LinearLayoutManager llm;


public FileViewerAdapter(Context context, LinearLayoutManager linearLayoutManager) {
    super();
    mContext = context;
    mDatabase = new DBHelper(mContext);
    mDatabase.setOnDatabaseChangedListener(this);
    llm = linearLayoutManager;
}

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

    item = getItem(position);
    long itemDuration = item.getLength();

    long minutes = TimeUnit.MILLISECONDS.toMinutes(itemDuration);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(itemDuration)
            - TimeUnit.MINUTES.toSeconds(minutes);

    holder.vName.setText(item.getName());
    holder.vLength.setText(String.format("%02d:%02d", minutes, seconds));
    holder.vDateAdded.setText(
            DateUtils.formatDateTime(
                    mContext,
                    item.getTime(),
                    DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_YEAR
            )
    );

    // define an on click listener to open PlaybackFragment
    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                PlaybackFragment playbackFragment =
                        new PlaybackFragment().newInstance(getItem(holder.getPosition()));

                FragmentTransaction transaction = ((FragmentActivity) mContext)
                        .getSupportFragmentManager()
                        .beginTransaction();

                playbackFragment.show(transaction, "dialog_playback");

            } catch (Exception e) {
                Log.e(LOG_TAG, "exception", e);
            }
        }
    });

    holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            ArrayList<String> entrys = new ArrayList<String>();
            entrys.add(mContext.getString(R.string.dialog_file_delete));

            final CharSequence[] items = entrys.toArray(new CharSequence[entrys.size()]);


            // File delete confirm
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle(mContext.getString(R.string.dialog_title_options));
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    if (item == 0) {
                        deleteFileDialog(holder.getPosition());
                    }
                }
            });
            builder.setCancelable(true);
            builder.setNegativeButton(mContext.getString(R.string.dialog_action_cancel),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

            AlertDialog alert = builder.create();
            alert.show();

            return false;
        }
    });
}

@Override
public RecordingsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.
            from(parent.getContext()).
            inflate(R.layout.card_view, parent, false);

    mContext = parent.getContext();

    return new RecordingsViewHolder(itemView);
}

public static class RecordingsViewHolder extends RecyclerView.ViewHolder {
    protected TextView vName;
    protected TextView vLength;
    protected TextView vDateAdded;
    protected View cardView;

    public RecordingsViewHolder(View v) {
        super(v);
        vName = (TextView) v.findViewById(R.id.file_name_text);
        vLength = (TextView) v.findViewById(R.id.file_length_text);
        vDateAdded = (TextView) v.findViewById(R.id.file_date_added_text);
        cardView = v.findViewById(R.id.card_view);
    }
}

@Override
public int getItemCount() {
    return mDatabase.getCount();
}

public RecordingItem getItem(int position) {
    return mDatabase.getItemAt(position);
}

0 个答案:

没有答案