I am new to Android and I am developing an application in which I have a ListView which displays reminders. I want to highlight a reminder when notification of that reminder is clicked. Now the problem is that; when there are two notifications then the notification which is clicked first is highlighted in the ListView, but when I click second notification it is highlighted in the ListView for a very short moment and again the previous notification is highlighted in the ListView. I am unable to identify why is this happening. So please help.
ListView Code:
package com.example.abc.smartlocator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Vector;
public class ViewReminders extends AppCompatActivity {
static ListView remindersList;
static Activity currentActivity;
static int scrollTo;
static String caller;
static CustomListView adapter;
static boolean isRuing = false;
@Override
protected void onNewIntent(Intent intent) {
caller = intent.getExtras().getString("CALLER");
if (caller.equals("GenNot")) {
scrollTo = intent.getExtras().getInt("ID");
remindersList.requestFocusFromTouch();
remindersList.setSelection(scrollTo);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_reminders);
isRuing = true;
currentActivity = this;
caller = getIntent().getExtras().getString("CALLER");
if(caller.equals("GenNot"))
scrollTo = getIntent().getExtras().getInt("ID");
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#141414")));
actionBar.setTitle("View Reminders");
TextView tv = new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(actionBar.getTitle());
tv.setTextColor(Color.parseColor("#ACACAC"));
tv.setTextSize(20);
tv.setTypeface(Typeface.create("MV Boli", Typeface.BOLD));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(tv);
remindersList = (ListView)findViewById(R.id.remindersList_id);
remindersList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
new FetchReminders(currentActivity).execute();
}
public static void displayReminders(Vector remindersAttributes, Vector remindersIds) {
if(remindersAttributes==null) {
Toast.makeText(currentActivity.getApplicationContext(), "Sorry! Reminders can't be displayed.", Toast.LENGTH_LONG).show();
currentActivity.finish();
}
else {
if (remindersAttributes.size() == 0) {
String[] values = {"No Reminder to Show!"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(currentActivity, R.layout.activity_empty_list, values);
remindersList.setSelector(R.color.emptyList);
remindersList.setAdapter(adapter);
}
else {
adapter = new CustomListView(currentActivity, R.layout.activity_list_view, remindersAttributes, remindersIds);
remindersList.setSelector(R.color.nonEmptyList);
remindersList.setAdapter(adapter);
if(caller.equals("GenNot")) {
remindersList.requestFocusFromTouch();
remindersList.setSelection(scrollTo);
}
}
}
}
}
Notification Code
package com.example.abc.smartlocator;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
public class GenerateNotification {
public static void reminderNotification(Context context, int notification_id, String document_id, String name, String date, Location location) {
Intent intent = new Intent(context, ViewReminders.class);
intent.putExtra("CALLER","GenNot");
intent.putExtra("ID", notification_id);
PendingIntent pIntent = PendingIntent.getActivity(context, notification_id, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setTicker("Smart Locator");
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle(name);
DetailsContainer dc = new LocationDetails(context).getDetails(location);
mBuilder.setContentText(date + ", " + dc.area + " " + dc.locality);
mBuilder.setContentIntent(pIntent).getNotification();
mBuilder.setAutoCancel(true);
mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(document_id, notification_id, mBuilder.build());
}
}
