我使用async
并且代码看起来很好,没有出现任何错误,但我的布局没有正确更新,这里出了什么问题?我尝试了与闹钟管理器的所有组合,但我的列表没有自动更新。
这是我的主要内容:
alarmmanager
这是我的适配器:
public class MainActivity extends AppCompatActivity implements UpdateResultReceiver.Receiver {
private static final String TAG = "MainActivity";
Data mData;
RecyclerView recyclerView;
NotificationManager manager;
static int x = 0;
RecyclerAdapter adapter;
private boolean isInited = false;
ArrayList<String> liste;
ArrayList<String> liste2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
init();
}
private void fillLists() {
new AsyncCaller().execute();
}
private void init() {
fillLists();
scheduleUpdater();
}
private class AsyncCaller extends AsyncTask<Void, Void, Data> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Data doInBackground(Void... params) {
try {
mData = new Data();
} catch (Exception e) {
Log.e(TAG, "doInBackground: ", e);
}
return mData;
}
@Override
protected void onPostExecute(Data data) {
super.onPostExecute(data);
if (mData == null) {
Toast.makeText(MainActivity.this, "Hata oluştu", Toast.LENGTH_SHORT).show();
return;
}
liste = data.getList();
liste2 = data.getList2();
int s = data.ListSize();
if (!isInited && liste != null && liste.size() > 0) {
adapter = new RecyclerAdapter(liste, liste2, MainActivity.this);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
isInited = true;
}
if (isInited) {
adapter.notifyDataSetChanged();
int a = adapter.getItemCount();
if (a != x) {
generateNotification(MainActivity.this, "Listede Degisim Var");
}
x = a;
}
}
}
private void generateNotification(Context context, String message) {
int notificationId = 001;
Intent viewIntent = new Intent(context, MainActivity.class);
PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.dikat).setContentTitle("Bildirim").setContentText(message)
.setContentIntent(viewPendingIntent).setAutoCancel(true)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId, notificationBuilder.build());
}
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
fillLists();
}
public void scheduleUpdater() {
try {
if (!isAlarmUp(this)) {
UpdateResultReceiver mReceiver = new UpdateResultReceiver(new Handler());
mReceiver.setReceiver(this);
/* Setting the alarm here */
Intent alarmIntent = new Intent(this, PatientListUpdaterReceiver.class);
alarmIntent.putExtra("receiver", mReceiver);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System
.currentTimeMillis(), 10000, pendingIntent);
}
} catch (Exception e) {
Log.e("asdads", "schedule: " + e.getMessage(), e);
}
}
public void cancelUpdater(Context context) {
try {
if (isAlarmUp(context)) {
Intent intent = new Intent(context, PatientListUpdaterReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
} catch (Exception e) {
Log.e("asdasd", "cancel: " + e.getMessage(), e);
}
}
private boolean isAlarmUp(Context context) {
return (PendingIntent
.getBroadcast(context, 0, new Intent(context, PatientListUpdaterReceiver.class), PendingIntent.FLAG_NO_CREATE) != null);
}
}
这是数据部分:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
int sayi=0;
ArrayList<String> liste ,liste2;
static ArrayList<String> liste3 = new ArrayList<>() ;
Context context;
LayoutInflater inflater;
public RecyclerAdapter(ArrayList<String> liste, ArrayList<String> liste2, Context context) {
this.context=context;
this.liste=liste;
this.liste2=liste2;
inflater=LayoutInflater.from(context);
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=inflater.inflate(R.layout.item_list, parent, false);
RecyclerViewHolder viewHolder=new RecyclerViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
liste3.add("a");
sayi = 0;
for (int j = 0; j < liste3.size(); j++) {
if (liste3.get(j).equals(liste.get(position))) {
sayi++;
}
if (sayi == 0) {
holder.imageView.setTag(holder);
} else {
holder.imageView.setImageResource(R.drawable.ic_checkin);
}
holder.tv1.setText(liste.get(position));
holder.tv2.setText(liste2.get(position));
holder.imageView.setOnClickListener(clickListener);
}
}
View.OnClickListener clickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
RecyclerViewHolder vholder = (RecyclerViewHolder) v.getTag();
int position = vholder.getPosition();
//Toast.makeText(context,"This is position "+position,Toast.LENGTH_LONG ).show();
vholder.imageView.setImageResource(R.drawable.ic_checkin);
liste3.add(liste.get(position));
}
};
@Override
public int getItemCount() {
return liste.size();
}
}
答案 0 :(得分:0)
问题是您是否更改了对数据的引用,因此适配器不会理解更改! 让我们举个例子:
List data=new ArrayList();
MyAdapter adapter=new MyAdapter(data);
稍后如果您希望适配器收到有关更改的通知,您应该只使用传递给适配器的引用!您应该将data
设为全局变量(私有,公共,您喜欢的任何内容),以便您可以从代码中的其他位置访问它。然后你只需更改其中的数据并添加或删除项目,然后调用notifydatasetchange。那就行了。