gpt4 book ai didi

java - 使用 ParseRecyclerQueryAdapter

转载 作者:行者123 更新时间:2023-11-30 01:41:59 25 4
gpt4 key购买 nike

我希望有人能帮助我指明使用 ParseRecyclerQueryAdapter 的正确方向。我正在尝试使用实现标准 RecyclerView 的当前适配器类检索文章。我的分页实现无法正常工作,所以我希望任何使用 ParseRecyclerQueryAdapter 实现以下目标的人都能进行演示。通过从我那里翻译它,所以我得到了实现。

public class ArticleRVAdapter extends RecyclerView.Adapter {

List<Article> articleData = Collections.emptyList();
private LayoutInflater inflater;
private Context context;

public ArticleRVAdapter(Context context, List<Article> data){
inflater=LayoutInflater.from(context);
this.articleData= data;
this.context = context;
}

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

View view = inflater.inflate(R.layout.article_item, parent, false);
ArticleViewHolder viewHolder = new ArticleViewHolder(view);
return viewHolder;
}

@Override
public int getItemCount() {
return articleData.size();
}

public class ArticleViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {

CardView cardView;
TextView title;
TextView body;
Date dateCreated;
TextView author;
ImageView headImage;
String objectId;

public ArticleViewHolder(final View itemView) {
super(itemView);
itemView.setClickable(true);
itemView.setLongClickable(true);
itemView.setOnClickListener(this);


cardView = (CardView) itemView.findViewById(R.id.card_article);
title = (TextView) itemView.findViewById(R.id.text_title);
body = (TextView) itemView.findViewById(R.id.text_article_body);
//dateCreated = itemView.findViewById(R.id.text_article_body);
author = (TextView) itemView.findViewById(R.id.text_author);
headImage = (ImageView) itemView.findViewById(R.id.imageView_thumb);


}
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ArticleInformationActivity.class);
intent.putExtra("article_id", articleData.get(getLayoutPosition()).getObjectId());
intent.putExtra("article_title", articleData.get(getLayoutPosition()).getArticleTitle());
intent.putExtra("article_body", articleData.get(getLayoutPosition()).getArticleBody());
intent.putExtra("article_author", articleData.get(getLayoutPosition()).getArticleAuthor());
intent.putExtra("article_date", articleData.get(getLayoutPosition()).getDateCreated());
v.getContext().startActivity(intent);

}
}

最佳答案

经过大量测试,在我的项目中得到了解决方案:

fragment (或 Activity ):

private RecyclerView mRecyclerView;
private MyQueryAdapter myQueryAdapter;
private SwipeRefreshLayout swipeRefreshLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.rv);
myQueryAdapter = new MyQueryAdapter(true);

mRecyclerView.setAdapter(myQueryAdapter);

mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

myQueryAdapter.addOnQueryLoadListener(new ParseRecyclerQueryAdapter.OnQueryLoadListener<MY_MODEL>() {
@Override
public void onLoaded(List<MY_MODEL> objects, Exception e) {
if (swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(false);
}
}

@Override
public void onLoading() {
if (!swipeRefreshLayout.isRefreshing()) {
swipeRefreshLayout.setRefreshing(true);
}

}
});



swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
myQueryAdapter.loadObjects();
}
});


return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

@Override
public void onResume() {
super.onResume();
myQueryAdapter.loadObjects();
}

适配器

public class MyQueryAdapter extends ParseRecyclerQueryAdapter<MY_MODEL, MyQueryAdapter.MyViewHolder>{


public MyQueryAdapter(boolean hasStableIds) {
super(MY_MODEL.class, hasStableIds);
}

public MarcacionesAdapter(ParseQueryAdapter.QueryFactory<Marcaciones> factory, boolean hasStableIds) {
super(factory, hasStableIds);
}



@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(v);
return myViewHolder;
}

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

MY_MODEL model = getItem(position);
holder.name.setText(model.getName());
holder.year.setText(model.getYear());

}

public static class MyViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView name;
TextView year;

MyViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.card_view);
name = (TextView)itemView.findViewById(R.id.name);
year = (TextView)itemView.findViewById(R.id.year);
}
}

}

如果你想配置一个自定义查询,你可以这样做:

       factory =
new ParseQueryAdapter.QueryFactory<MY_MODEL>() {
public ParseQuery< MY_MODEL > create() {

ParseQuery<MY_MODEL> query = MY_MODEL.getQuery();
query.include("user");

//Filter by date
query.whereGreaterThan("createdAt", midnight);
query.whereLessThan("createdAt", now);

query.orderByDescending("createdAt");


return query;
}
};

adapter = new MyQueryAdapter(factory, true);

关于java - 使用 ParseRecyclerQueryAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34370739/

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