gpt4 book ai didi

android - 从 RecyclerAdapter 获取数据

转载 作者:太空狗 更新时间:2023-10-29 13:55:37 26 4
gpt4 key购买 nike

我有一个带 fragment 的抽屉导航

  • 其中一个 fragment 是 WatchListFragment,它包含一个 RecyclerView,其中填充了一个剧集列表。

  • listRecyclerAdapter 相连。在 RecyclerAdapter 中,我有一个 onLongClickListener,它的结果应该是更改列表中的某些剧集,并用它替换我在 WatchListFragment 中的列表。

但我不知道如何将数据传递到我的 WatchListFragment。在 c++ 中,id 只是通过引用传递列表,但这在这里不起作用。有任何想法吗?

fragment 代码:

public class WatchListFragment extends Fragment {
private EpisodeRecycleAdapter mAdapter;
private List<Episode> releasedEpisodeList; //contains all unseen & released episodes
private RecyclerView mRecyclerView;
private Context context;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

View view = inflater.inflate(R.layout.watch_list_fragment, container, false);
context = view.getContext();

mRecyclerView = (RecyclerView) view.findViewById(R.id.watchlistRV);
/* improved performance if size of the layout doesnt change */
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
mRecyclerView.setLayoutManager(mLayoutManager);

//load watchlist from saved file
ejson = new EpisodeJSON(context);
watchList = ejson.readFromFile();

wlFunc = new WatchListFunctions();

//load released episodes of watchlist in releasedEpisodeList
releasedEpisodeList = wlFunc.getReleasedEpisodeList(watchList);
mAdapter = new EpisodeRecycleAdapter(context, releasedEpisodeList);
mRecyclerView.setAdapter(mAdapter);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.watchlist_options, menu);
}

RecyclerAdapter 代码:

class EpisodeRecycleAdapter extends RecyclerView.Adapter<EpisodeRecycleAdapter.ViewHolder> {
private List<Episode> episodes = new ArrayList<>();
private Context mContext;

EpisodeRecycleAdapter(Context context, List<Episode> myDataset) {
super();
this.episodes = myDataset;
mContext = context;
}

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
TextView textViewName, textViewNumbers, textViewDate;
ImageView imageViewEye;

ViewHolder(View v) {
super(v);
textViewName = (TextView)v.findViewById(R.id.showname);
textViewNumbers = (TextView)v.findViewById(R.id.seasonepisodenumbers);
textViewDate = (TextView)v.findViewById(R.id.date);
imageViewEye = (ImageView) v.findViewById(R.id.eye_image);
}
}

// Create new views (invoked by the layout manager)
@Override
public EpisodeRecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem_row, parent, false);

v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
RecyclerView mRecyclerView = (RecyclerView) view.getRootView().findViewById(R.id.watchlistRV);
int itemPosition = mRecyclerView.getChildLayoutPosition(view);
final Episode episodeItem = episodes.get(itemPosition);
final int interval = episodeItem.getInterval();
final LocalDate episodeDate = episodeItem.getDate();

AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
alert.setTitle("title");
alert.setMessage("msg");

String dateStringArray[] = new String[52];
for (int i = 0; i < 52; i++) {
dateStringArray[i] = episodeDate.plusDays(interval * (i+1)).toString("dd.MM.yy");
}
final Spinner picker = new Spinner(mContext);
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, dateStringArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
picker.setAdapter(adapter);
alert.setView(picker);
alert.show();

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
int i = 1;
int eId = episodeItem.getSeasonID();
for(Episode e: episodes){
if(e.getSeasonID() == eId && !e.getDate().isBefore(episodeItem.getDate())){
e.setDate(e.getDate().plusDays(interval * i));
i++;
}
}
}
});
//TODO get episodes in watchlistfragment

alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});

return true; //return true so no onClick event happens
}
});
return new ViewHolder(v);
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int pos) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.textViewName.setText(episodes.get(pos).getShowName());
holder.textViewNumbers.setText(episodes.get(pos).getSeasonEpisodeAsString());
holder.textViewDate.setText(episodes.get(pos).getDateAsString());
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return episodes.size();
}

最佳答案

像这样在你的适配器中创建一个方法

public static List<Episode> getList(){
return episodes ;
}

然后像这样在你的 fragment 中调用这个方法

mAdapter.getList(); // List<Episode> after changes

您可以使用的另一种方法

你可以这样做releasedEpisodeList = episodes;在佛r(Episode e: episodes){ if(e.getSeasonID() == eId && !e.getDate().isBefore(episodeItem.getDate())){ e.setDate(e.getDate().plusDays(interval * i)); i++; } }releasedEpisodeList = episodes;之后为此制作private List<Episode> releasedEpisodeList;进入public static List<Episode> releasedEpisodeList

关于android - 从 RecyclerAdapter 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39957304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com