Ayrıntılı sayfaya gittiği üniversiteyi görüntülemek ve tıklamak için Kolejler listesi yapıyorum. Orada bildirim ve Favourite_icon
ile bir menü çubuğu var. Ben Favourite_icon
ve Favourite_icon1
yaşıyorum. Kullanıcılar, favourite_icon
'u clcikse, sunucuda favoriler olarak saklanır ve simge Favourite_icon1
olarak değiştirilir. Bazı işlemler yaptıktan sonra Favourited kolejini okudum. Bu süre, menü çubuğunda Favourite_icon1
'u göstermelidir. Aşağıdaki yöntemi denedim ama hiçbir şey olmadı. Ben denedim kod eklemişBaşlangıçta sunucu yanıtına göre menü çubuğu simgesi nasıl ayarlanır
bu kolej zaten onCreate()
yılında favourited ya da olmasın her türlü hava şartlarına çek kodudur
private boolean canAddItem;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_clg, menu);
mMenu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
navigatetoNotification();
return true;
case R.id.action_favourite:
if (item.getItemId() == R.id.action_favourite) {
invalidateOptionsMenu();
favouriteClg();
}
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (canAddItem) {
menu.getItem(1).setIcon(R.drawable.vijay);
canAddItem = false;
favouriteClg();
} else {
menu.getItem(1).setIcon(R.drawable.favourite_icon);
canAddItem = true;
favouriteClg();
}
return super.onPrepareOptionsMenu(menu);
}
menu_clg.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appmunu="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".UserDashBoardFragment">
<item
android:id="@+id/action_notify"
android:icon="@drawable/mail_icon"
appmunu:showAsAction="always"
android:title="Notification" />
<item
android:id="@+id/action_favourite"
android:icon="@drawable/icon_selector"
appmunu:showAsAction="always"
android:title="Favourite" />
</menu>
bu Etkinlik kodu yöntem
public void chechFavourite() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... arg0) {
//method name changed here
//getAllEventFromUser method used for getting all previously send events of current user
return favouriteDelegates.getAllCollegeDetails(userMO, context);
}
@Override
protected void onPostExecute(String collegelists) {
if (collegelists != "null") {
initCollegeMO = gson.fromJson(collegelists, new TypeToken<InitCollegeMO>() {
}.getType());
collegeMOs = initCollegeMO.getCollegeMOs();
for (CollegeMO collegeMO1 : collegeMOs) {
//here the list of college has eceived from server so i checked all the college id with current college id
collegeId = collegeMO1.getCollegeId(); //here collegeMO isthe object which is accessed by parcelable from another activity
if (collegeMO.getCollegeId() == collegeId) {
canAddItem = true;
} else {
canAddItem = false;
}
}
} else {
canAddItem = false;
}
}
}.execute(null, null, null);
}
}
i onun zaten favori ya da her türlü hava şartlarına kontrol etmek gerekmez i bu updateMenuTitles eklemek gereken yerde (edit 1 –
@vijayg favourite_icon aksi favourite_icon1 ayarlamanız gerekir favourited varsa) yöntem. oncreate() içinde? –
kontrol –