我有一个录音机应用程序。它会在片段中录制声音。我想在recyclerview中的另一个片段中显示记录的项目。现在发生的事情是记录器工作正常。我已经看到数据被插入数据库中。但记录的项目不会显示在recyclerview中。我想在数据插入后立即在recyclerview上显示重新排列的项目。
这是我的适配器类---
public class FileViewerAdapter extends RecyclerView.Adapter<FileViewerAdapter.RecordingViewHolder> implements OnDatabaseChangedListener
{
Context mContext;
DBHelper mDatabase;
RecordingItem item;
LinearLayoutManager llm;
private static final String LOG_TAG = "FileViewerAdapter";
@Override
public FileViewerAdapter.RecordingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
mContext=parent.getContext();
return new RecordingViewHolder(v);
}
public FileViewerAdapter(Context context, LinearLayoutManager linearLayoutManager) {
super();
mContext = context;
mDatabase = new DBHelper(mContext);
mDatabase.setOnDatabaseChangedListener(this);
llm = linearLayoutManager;
}
@Override
public void onBindViewHolder(final FileViewerAdapter.RecordingViewHolder holder, final int position) {
item=getItem(position);
int itemDuration=item.getLength();
int minute= (int) TimeUnit.MILLISECONDS.toMinutes(itemDuration);
int second= (int) ( TimeUnit.MILLISECONDS.toSeconds(itemDuration)-TimeUnit.MINUTES.toSeconds(minute));
holder.vName.setText(item.getName());
holder.vLength.setText(String.format("%02d:%02d", minute, second));
holder.vDateAdded.setText(
DateUtils.formatDateTime(
mContext,
item.getTime(),
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_YEAR));
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
PlaybackFragment playbackFragment =
new PlaybackFragment().newInstance(getItem(holder.getPosition()));
FragmentManager fm = ((FragmentActivity) mContext)
.getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
playbackFragment.show(fm,"dialog playback");
ft.commit();
}catch(Exception e)
{
Log.e(LOG_TAG,"exception",e);
}
}
});
holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ArrayList<String> arrayList=new ArrayList<String>() ;
arrayList.add(mContext.getString(R.string.dialog_file_share));
arrayList.add(mContext.getString(R.string.dialog_file_rename));
arrayList.add(mContext.getString(R.string.dialog_file_delete));
final CharSequence[] items= arrayList.toArray(new CharSequence[arrayList.size()]);
AlertDialog.Builder builder=new AlertDialog.Builder(mContext);
builder.setTitle(mContext.getString(R.string.dialog_title_options));
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
if(position==0)
{
shareFileDialog(holder.getPosition());
}
if(position==1)
{
renameFileDialog(holder.getPosition());
}
if(position==2)
{
deleteFileDialog(holder.getPosition());
}
}
});
builder.setCancelable(true);
builder.setNegativeButton(mContext.getString(R.string.dialog_action_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert= builder.create();
alert.show();
return false;
}
});
}
@Override
public int getItemCount() {
return mDatabase.getCount();
}
}
这是片段类的'OnCreateView'方法,其中将显示recyclerview ----
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_file_viewer, container, false);
RecyclerView mRecyclerView = (RecyclerView) v.findViewById(R.id.recycleView);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
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(getActivity(), llm);
mRecyclerView.setAdapter(mFileViewerAdapter);
mFileViewerAdapter.notifyDataSetChanged();
return v;
}
那么,问题是什么?请有人帮忙!!!
答案 0 :(得分:2)
通过查看您的理解,您还没有正确实施recyclerview适配器。
从数据库中选择数据并将其存储在Arraylist中并将其传递给适配器,然后从中访问数据并将其设置为VIews。
将数据插入数据库(我希望SQLite)后,您必须再次获取数据并将其添加到Arraylist并通过调用adapter.notifyDataSetChanged()更新Recyclerview;来自活动/片段。
通过这种方式,您可以实现干净且可维护的代码。