public class MyAdapterMarket extends RecyclerView.Adapter<MyAdapterMarket.MyViewHolder> {
Context c;
ArrayList<MarketObject> marketObject;
public RecyclerView recyclerView;
public MyAdapterMarket(MarketActivity.MyFragment c, ArrayList<MarketObject> marketObject) {
this.marketObject = marketObject;
}
// I AM ABLE TO GET UPDATED OBJECT HERE BUT HOW CAN I UPDATE IN RECYCLEVIEW AT PARTICULAR POSITION. // marketObject.getLast() gives updated value in Realm Database
public static void updateEmployeeListItems(MarketObject marketObject) {
Log.e("UPDATED OBJECT FROM REALM DATABASE",""+marketObject.getLast());
}
@Override
public MyAdapterMarket.MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.calender_market_list, parent, false);
return new MyAdapterMarket.MyViewHolder(v);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView market_title, country_name, market_last, market_time, market_dailyPercentageChange, market_dailyChange;
public LinearLayout actual, previous, constant;
public MyViewHolder(View itemView) {
super(itemView);
c = itemView.getContext();
recyclerView = (RecyclerView) itemView.findViewById(R.id.calendar_fragment_recycleview);
market_title = (TextView) itemView.findViewById(R.id.market_title);
country_name = (TextView) itemView.findViewById(R.id.country_name);
market_last = (TextView) itemView.findViewById(R.id.market_last);
market_time = (TextView) itemView.findViewById(R.id.market_time);
market_dailyPercentageChange = (TextView) itemView.findViewById(R.id.market_dailyPercentageChange);
market_dailyChange = (TextView) itemView.findViewById(R.id.market_dailyChange);
}
}
@Override
public void onBindViewHolder(MyAdapterMarket.MyViewHolder holder, final int position) {
final MarketObject market = marketObject.get(position);
holder.market_title.setText(market.getName());
holder.country_name.setText(capitalize(market.getCountry()));
holder.market_last.setText(market.getLast());
holder.market_time.setText(market.getTime());
holder.market_dailyPercentageChange.setText(market.getDailypercentualchange());
holder.market_dailyChange.setText(market.getDailychange());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Log.v("RECYCLEVIEW POSITION", "Element " + getAdapterPosition() + " clicked.");
AutoUpdate(position, "0.007");
Context context = v.getContext();
Intent intent = new Intent(context, MarketDetail.class);
intent.putExtra( "NAME" , market.getName());
//context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return marketObject.size();
}
}
给代码是我的适配器代码,现在我得到市场的实时数据,它在Realm数据库中与MarketObject完美更新。我想在Recycleview中反映这些数据还有动画,这样用户就可以看到这个市场价格更新了。怎么可能。在Realm Database中更新此值时,我在此函数“updateEmployeeListItems()”中传递Object。现在我无法在特定位置更新RecycleView。
答案 0 :(得分:2)
好的,如果您想利用Realm的细粒度通知,那么您应该使用RealmRecyclerViewAdapter,并且最重要的是
public class MyAdapterMarket extends RealmRecyclerViewAdapter<MarketObject, MyAdapterMarket.MyViewHolder> {
public MyAdapterMarket(OrderedRealmCollection<MarketObject> results {
super(results, true, false); // passing `false` to handle `change` manually
}
@Override
public MyAdapterMarket.MyViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
return new MyAdapterMarket.MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.calender_market_list, parent, false));
}
public class MyViewHolder extends RecyclerView.ViewHolder {
Context c;
RecyclerView recyclerView;
TextView market_title, country_name, market_last, market_time, market_dailyPercentageChange, market_dailyChange;
LinearLayout actual, previous, constant;
public MyViewHolder(View itemView) {
super(itemView);
c = itemView.getContext();
recyclerView = (RecyclerView) itemView.findViewById(R.id.calendar_fragment_recycleview);
market_title = (TextView) itemView.findViewById(R.id.market_title);
country_name = (TextView) itemView.findViewById(R.id.country_name);
market_last = (TextView) itemView.findViewById(R.id.market_last);
market_time = (TextView) itemView.findViewById(R.id.market_time);
market_dailyPercentageChange = (TextView) itemView.findViewById(R.id.market_dailyPercentageChange);
market_dailyChange = (TextView) itemView.findViewById(R.id.market_dailyChange);
}
RealmObjectChangeListener realmChangeListener = new RealmObjectChangeListener<MarketObject>() {
@Override
public void onChange(MarketObject market, ObjectChangeSet changeSet) {
if(changeSet.isDeleted()) return;
if(changeSet.isFieldChanged("name")) {
market_title.setText(market.getName());
}
if(changeSet.isFieldChanged("country")) {
country_name.setText(capitalize(market.getCountry()));
}
if(changeSet.isFieldChanged("last")) {
market_last.setText(market.getLast());
}
if(changeSet.isFieldChanged("time")) {
market_time.setText(market.getTime());
}
if(changeSet.isFieldChanged("dailypercentualchange")) {
market_dailyPercentageChange.setText(market.getDailypercentualchange());
}
if(changeSet.isFieldChanged("dailychange")) {
market_dailyChange.setText(market.getDailychange());
}
}
}
MarketObject market;
public void bind(MarketObject market) {
if(this.market != null && this.market.isValid()) {
this.market.removeAllChangeListeners();
}
this.market = market;
market.addChangeListener(realmChangeListener);
market_title.setText(market.getName());
country_name.setText(capitalize(market.getCountry()));
market_last.setText(market.getLast());
market_time.setText(market.getTime());
market_dailyPercentageChange.setText(market.getDailypercentualchange());
market_dailyChange.setText(market.getDailychange());
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Log.v("RECYCLEVIEW POSITION", "Element " + getAdapterPosition() + " clicked.");
AutoUpdate(position, "0.007");
Context context = v.getContext();
Intent intent = new Intent(context, MarketDetail.class);
intent.putExtra( "NAME" , market.getName());
//context.startActivity(intent);
}
});
}
}
@Override
public void onBindViewHolder(MyAdapterMarket.MyViewHolder holder, final int position) {
final MarketObject market = marketObject.get(position);
holder.bind(market);
}
}
答案 1 :(得分:0)
如果您只更新了一个项目,则可以使用新项目更新您的支持列表(即在旧项目的索引处替换它),然后通过调用{{1}告诉RecyclerView
适配器有关更改的信息。 },其中recyclerView.getAdapter().notifyItemChanged(POSITION)
是列表/数组中更新项的整数索引。
如果您获得了一个全新的列表项目,您可以使用POSITION
查找旧的支持项目列表和更新的支持项目列表之间的差异,然后发送对DiffUtil
adatper的更改:
话虽这么说,用户@Saran Sankaran建议使用RealmRecyclerViewAdapter
可能就是你要找的东西。如果您使用它,则不需要自己执行任何RecyclerView
逻辑。