I have created navigation drawer and displaying items in list. My list is static but i am displaying points. So when users select the items in the list and do some activity he get the points. So i want to update the points as soon as he gets. How can i do this./
Below is my code:
Activity Class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
// Initializing
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
sharedPreferences = getSharedPreferences(Constant.LOGIN_CHECK,
Constant.PRIVATE_MODE);
editSharedPreferences = sharedPreferences.edit();
if (sharedPreferences.getBoolean(Constant.IS_ALREADY_LOGIN,
false)) {
loginInBackground();
}
String p1 = sharedPreferences.getString(Constant.USERS_POINTS,
"");
String p2 = sharedPreferences.getString(
Constant.LEVEL_ONE_POINTS, "");
String p3 = sharedPreferences.getString(
Constant.LEVEL_TWO_POINTS, "");
String pointText = " POINTS";
dataList.add(new DrawerItem("OVERVIEW", R.drawable.ic_action_eye,
Color.DKGRAY, Color.TRANSPARENT));
dataList.add(new DrawerItem("EARN POINTS", R.drawable.ic_action_dollor,
Color.GREEN, Color.TRANSPARENT));
dataList.add(new DrawerItem("MY POINTS ", p1 + pointText, Color.BLACK,
Color.BLACK));
dataList.add(new DrawerItem("REFERRALS POINTS LV1 ", p2 + pointText,
Color.BLACK, Color.BLACK));
dataList.add(new DrawerItem("REFERRALS POINTS LV2 ", p3 + pointText,
Color.BLACK, Color.BLACK));
dataList.add(new DrawerItem("WITHDRAW REWARDS",
R.drawable.ic_action_home, Color.DKGRAY, Color.TRANSPARENT));
dataList.add(new DrawerItem("MY PROFILE", R.drawable.ic_action_settings,
Color.DKGRAY, Color.TRANSPARENT));
dataList.add(new DrawerItem("HELP", R.drawable.help_d, Color.DKGRAY,
Color.TRANSPARENT));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
dataList);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
SelectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
String pointsLeft = sharedPreferences.getString(
Constant.POINTS_LEFT, "");
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
MenuItem pointsMenu = menu.findItem(R.id.action_settings);
pointsMenu.setTitle(pointsLeft + " Points");
return true;
}
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 0:
fragment = new FragmentWelcome();
args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
dataList.get(possition).getImgResID());
fragment.setArguments(args);
FragmentManager frgManager0 = getFragmentManager();
frgManager0.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
break;
case 1:
fragment = new FragmentAppList();
args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
dataList.get(possition).getImgResID());
fragment.setArguments(args);
FragmentManager frgManager1 = getFragmentManager();
frgManager1.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
break;
case 5:
fragment = new FragmentRedeemPoints();
args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
dataList.get(possition).getImgResID());
fragment.setArguments(args);
FragmentManager frgManager2 = getFragmentManager();
frgManager2.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
break;
case 6:
fragment = new SettingsFragment();
args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
dataList.get(possition).getImgResID());
fragment.setArguments(args);
FragmentManager frgManager6 = getFragmentManager();
frgManager6.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
break;
case 7:
fragment = new HelpFragment();
args.putString(FragmentWelcome.ITEM_NAME, dataList.get(possition)
.getItemName());
args.putInt(FragmentWelcome.IMAGE_RESOURCE_ID,
dataList.get(possition).getImgResID());
fragment.setArguments(args);
FragmentManager frgManager7 = getFragmentManager();
frgManager7.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
break;
default:
break;
}
mDrawerList.setItemChecked(possition, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
SelectItem(position);
}
}
CustomDrawerAdapter:
public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
public CustomDrawerAdapter(Context context, int layoutResourceID,
List<DrawerItem> listItems) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
}
@SuppressWarnings("static-access")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_itemName);
drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
drawerHolder.pointsText = (TextView) view
.findViewById(R.id.get_login);
drawerHolder.divider = view.findViewById(R.id.dividerView);
drawerHolder.itemLayoutColor = (LinearLayout) view
.findViewById(R.id.itemLayoutColor);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
if (dItem.getPointText() == null) {
drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
dItem.getImgResID()));
drawerHolder.pointsText.setVisibility(view.GONE);
} else {
drawerHolder.pointsText.setText(dItem.getPointText());
}
drawerHolder.itemLayoutColor.setBackgroundColor(dItem.layoutColorCode);
drawerHolder.divider.setBackgroundColor(dItem.colorCode);
drawerHolder.ItemName.setText(dItem.getItemName());
return view;
}
private static class DrawerItemHolder {
TextView ItemName;
ImageView icon;
TextView pointsText;
View divider;
LinearLayout itemLayoutColor;
}
}
Finally i have fixed this by using below code. I want to answer to this so that others can get the reference from this. @user3154663. May be this will help you to solve your problem:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
if (drawerOpen) {
dataList.remove(2);
p1 = sharedPreferences.getString(Constant.USERS_POINTS, "");
p2 = sharedPreferences.getString(Constant.LEVEL_ONE_POINTS,
"");
p3 = sharedPreferences.getString(Constant.LEVEL_TWO_POINTS,
"");
String pointText = " POINTS";
dataList.add(2, new DrawerItem("MY POINTS ", p1 + pointText,
Color.BLACK, Color.BLACK));
dataList.remove(3);
dataList.add(3, new DrawerItem("REFERRALS POINTS LV1 ", p2
+ pointText, Color.BLACK, Color.BLACK));
dataList.remove(4);
dataList.add(4, new DrawerItem("REFERRALS POINTS LV2 ", p3
+ pointText, Color.BLACK, Color.BLACK));
adapter.notifyDataSetChanged();
mDrawerList.setAdapter(adapter);
}
return super.onPrepareOptionsMenu(menu);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With