我有 RecyclerView ,它在片段中显示 ArrayList 项。
项目有一个字符串,每秒都需要更新。
如何每秒更新所有 ArrayList 项目中的字符串,并立即在 RecyclerView 中显示更改?
片段,其中显示 RecyclerView :
public class RecyclerViewFragment extends Fragment{
DbActivity db;
Cursor fullData;
View v;
ConstraintLayout addDeadline;
CardView empty;
RecyclerView recyclerView;
List<Deadline> deadlines;
DeadlineAdapter deadlineAdapter;
String id, summary, date, time, deadline, labels;
public RecyclerViewFragment() { // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_recycler_view, container, false);
setBasicView();
addDeadline();
return v;
}
public void setBasicView(){
recyclerView = v.findViewById(R.id.recycler_view);
empty = v.findViewById(R.id.empty);
db = new DbActivity(getContext());
fullData = db.getAllData();
if (fullData.getCount() > 0){
if (fullData.moveToFirst()) {
deadlines = new ArrayList<>();
int i = 1;
do {
String check = fullData.getString(5);
if (!check.startsWith("co")){
id = fullData.getString(1);
summary = fullData.getString(2);
date = fullData.getString(3);
time = fullData.getString(4);
deadline = fullData.getString(5);
labels = fullData.getString(6);
deadlines.add(new Deadline(id, summary, date, time, deadline,
labels));
i++;
}
} while (fullData.moveToNext());
}
empty.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
}
else {
deadlines = new ArrayList<>();
recyclerView.setVisibility(View.GONE);
empty.setVisibility(View.VISIBLE);
}
deadlineAdapter = new DeadlineAdapter(getContext(), deadlines);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(deadlineAdapter);
}
public void addDeadline(){
addDeadline = v.findViewById(R.id.add_deadline);
addDeadline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getContext(), AddDeadlineActivity.class);
startActivityForResult(intent, Codes.INTENT_REQUEST_CODE);
}
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
mMessageReceiver,
new IntentFilter("AddDeadline"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
empty.setVisibility(View.GONE);
DbActivity db = new DbActivity(getContext());
Cursor newDeadline = db.getAllData();
int position = intent.getIntExtra("position", -1);
try {
if (position != -1){
newDeadline.moveToPosition(position);
id = newDeadline.getString(1);
summary = newDeadline.getString(2);
date = newDeadline.getString(3);
time = newDeadline.getString(4);
deadline = newDeadline.getString(5);
labels = newDeadline.getString(6);
deadlines.remove(position);
deadlines.add(position, new Deadline(String.valueOf(position), summary, date, time, deadline, labels));
}
else {
newDeadline.moveToLast();
id = newDeadline.getString(1);
summary = newDeadline.getString(2);
date = newDeadline.getString(3);
time = newDeadline.getString(4);
deadline = newDeadline.getString(5);
labels = newDeadline.getString(6);
deadlines.add(new Deadline(id, summary, date, time, deadline, labels));
}
}
catch (NullPointerException e){
newDeadline.moveToLast();
}
deadlineAdapter.notifyDataSetChanged();
recyclerView.setVisibility(View.VISIBLE);
}
};
@Override
public void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
自定义适配器:
public class DeadlineAdapter extends RecyclerView.Adapter<DeadlineAdapter.mViewHolder> {
Context context;
List<Deadline> deadlines;
public DeadlineAdapter(Context context, List<Deadline> deadlines) {
this.context = context;
this.deadlines = deadlines;
}
@Override
public mViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new mViewHolder(LayoutInflater.from(context).inflate(R.layout.recycler_view_item, parent, false));
}
@Override
public void onBindViewHolder(final mViewHolder holder, int position) {
holder.summary.setText(deadlines.get(position).getSummary());
holder.deadline.setText(deadlines.get(position).getDeadline());
holder.labels.setText(String.valueOf(deadlines.get(position).getLabels()));
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
editDeadline(position);
}
});
}
private void editDeadline(int position) {
Intent intent = new Intent(context, AddDeadlineActivity.class);
intent.putExtra("id", deadlines.get(position).getId());
intent.putExtra("summary", deadlines.get(position).getSummary());
intent.putExtra("date", deadlines.get(position).getDate());
intent.putExtra("time", deadlines.get(position).getTime());
intent.putExtra("labels", deadlines.get(position).getLabels());
intent.putExtra("position", position);
context.startActivity(intent);
}
@Override
public int getItemCount() {
return deadlines.size();
}
public static class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private ItemClickListener itemClickListener;
private TextView summary;
private TextView deadline;
private TextView labels;
public mViewHolder(View itemView) {
super(itemView);
summary = itemView.findViewById(R.id.customSummary);
deadline = itemView.findViewById(R.id.customDeadline);
labels = itemView.findViewById(R.id.customLabels);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v, getAdapterPosition(), false);
}
@Override
public boolean onLongClick(View v) {
itemClickListener.onClick(v, getAdapterPosition(), true);
return true;
}
}
}
任何帮助将不胜感激。
答案 0 :(得分:1)
知道了。
首先,我们需要在自定义适配器内部创建一个方法,该方法将更改列表中所有项目的数据。
1步。例如,
public void timer() {
String stringToChange;
for(int i = 0; i < yourList.size(); i++) {
//change data depending on time
//set new data to the deadline field
yourList.get(i).setData(stringToChange);
notifyDataSetChanged();
}
}
然后我们应该在片段或列表所在的活动中创建一个Handler。
2步示例:
public void setHandler(){
final Handler handler = new Handler();
final int delay = 1000 ; //1000 milliseconds = 1 sec
handler.postDelayed(new Runnable(){
public void run(){
customAdapter.timer(); // call our adapter method here
handler.postDelayed(this, delay);
}
}, delay);
}
3步是在OnCreate(OnCreateView for fragments)中调用setHandler方法:
setHandler();