I am unable to refresh my ListView in ListFragment with new data. Instead the new data is added to the previous.
The time period is from 6AM to 5PM for each entity. Then new data is appended to the list restarting at 6AM for another entity. The data for the first entity should be cleaned before the second is added to the ListView.
Here is the code:
public class FragmentStatePagerSupport extends FragmentActivity {
static final int NUM_ITEMS = 4; //control number of fragments
MyAdapter mAdapter;
ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(null);
mPager.setAdapter(mAdapter);
}
//===============================================================================================
public static class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
fragNumber = position;
return ArrayListFragment.newInstance(position);
}
}
//===============================================================================================
public static class ArrayListFragment extends ListFragment {
Integer mNum;
String FORMAT_LINE = "%s%7s%7s%10s%16s";
static ArrayListFragment newInstance(int num) {
ArrayListFragment f = new ArrayListFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
View tv = v.findViewById(R.id.text);
View tvd = v.findViewById(R.id.tv_description);
String title = "";
switch (mNum){
case 0:title = MyGlobals.getInstance().getToday();break;
case 1:title = MyGlobals.getInstance().getTomorrow();break;
case 2:title = MyGlobals.getInstance().getDayAfter();break;
case 3:title = MyGlobals.getInstance().getDayDayAfter();break;
}
((TextView) tv).setText(MyGlobals.getInstance().getName() + " on " + title);
((TextView) tvd).setText(String.format(FORMAT_LINE, "time", "temp", "rain", "wind", "weather"));
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<Data> row = Data.getRows(mNum);
ListViewAdapter adapter = new ListViewAdapter(getActivity(), row);
getListView().setAdapter(null);
getListView().setAdapter(adapter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
v.setBackgroundColor(getResources().getColor(R.color.blue));
}
}
//==============================================================================================
public static class ListViewAdapter extends ArrayAdapter<Data> {
public static class ViewHolder{
TextView time;
TextView temp;
TextView rain;
TextView wind_speed;
TextView weather;
}
public ListViewAdapter(Context context, ArrayList<Data> list) {super(context, R.layout.text_listview, list); }
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Data data = getItem(position);
ViewHolder holder;
if(convertView == null){
holder=new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView=inflater.inflate(R.layout.text_listview,parent,false);
holder.time=(TextView) convertView.findViewById(R.id.time);
holder.temp=(TextView) convertView.findViewById(R.id.temp);
holder.rain=(TextView) convertView.findViewById(R.id.rain);
holder.wind_speed=(TextView) convertView.findViewById(R.id.wind);
holder.weather=(TextView) convertView.findViewById(R.id.weather);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.time.setText(data.time);
holder.temp.setText(data.temp);
holder.rain.setText(data.rain);
holder.wind_speed.setText(data.wind_speed);
holder.weather.setText(data.weather);
return convertView;
}
}
}
This last piece populates the ListView. The calls adapter.clear() and adapter.notifyDataSetChanged() does not resolve the problem.
There is a similar question from 3 years ago How update ListView in ListFragment from FragmentActivity? still without accepted response despite the 6836 views.
Thanks a Lot.
The GetRows(mNum) piece as requested:
public class Data {
public String time;
public String temp;
public String rain;
public String wind_speed;
public String weather;
public Data(String time, String temp, String rain, String wind_speed, String weather) {
this.time = time;
this.temp = temp;
this.rain = rain;
this.wind_speed = wind_speed;
this.weather = weather;
}
public static ArrayList<Data> getRows(int fragNumber) {
int mNum = fragNumber;
int size;
String myList[] = null;
ArrayList<Data> list = new ArrayList<Data>();
switch (mNum) {
case 0://Today
size = MyGlobals.getInstance().getToday("time").size();
myList = new String[size];
for (int i = 0; i < size; i++) {
String time = (String) MyGlobals.getInstance().getToday("time").get(i);
String temp = (String) MyGlobals.getInstance().getToday("temperature").get(i);
String wind_speed = (String) MyGlobals.getInstance().getToday("wind_speed").get(i);
String pop = (String) MyGlobals.getInstance().getToday("pop").get(i);
//String wind_gust = (String) MyGlobals.getInstance().getToday("wind_gust").get(i); //when null breaks code
String weather = (String) MyGlobals.getInstance().getToday("weather").get(i);
if (time.length() == 4) time = "0" + time;
if (wind_speed.length() == 1) wind_speed = "0" + wind_speed;
if (pop.length() == 1) pop = "0" + pop;
list.add(new Data(time,temp,pop,wind_speed,weather));
}
return list;
case 1://Tomorrow
...snip... same as above with pertinent variables,...
case 2://DayAfter
...snip...
case 3://DayDayAfter
...snip....
case 4:// is an error
Log.d("***error***", "list got to case 5");
}
return list;
}
}
Try this;
Before adding new list of Data to the existing list, use list.clear();
Add new Data to the list and you should have only newly added data in the list..
pass the list to your adapter class.
Notify adapter of the new data i.e. adapter.notifyDataSetChanged()
You are returning list twice in your getRows()
remove the "return list" before the case structure
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
// Clear old list here before adding new one.
if (list! = null)
{
list.clear();
}
// Add New list
list = Data.getRows(mNum);
ListViewAdapter adapter = new
ListViewAdapter(getActivity(), list);
getListView().setAdapter(adapter);
adapter.notifyDataSetChange();
}
It's because everytime you set an Adapter, it will really be added., remove all its data first before setting again so it will be like
mPager.setAdapter(null);
before doing
mPager.setAdapter(mAdapter);
Related
I have an Alarm app which contain 3 tabs (3 Fragments) , First one is "Edit" , And second one is "Alarm" which display a list-view of all alarms , Finally last tab is "Add Alarm" i have time-picker and button , i want when i pick a time and click on add button take that values to tab "alarm" and add it to list-view.
But in my code ,It just make 1 item , And when i add one , it replace old with new one , So it don't add it just update .
After day of searching i am really tired :(
Can anyone help me please .
Here is my full code.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String getHourFromAddPage = intent.getStringExtra("HOURS_ADD_PAGE");
String getFormatFromAddPage = intent.getStringExtra("FORMAT_ADD_PAGE");
String getDaysFromAddPage = intent.getStringExtra("DAYS_ADD_PAGE");
String getLabelFromAddPage = intent.getStringExtra("LABEL_ADD_PAGE");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager() , getHourFromAddPage , getFormatFromAddPage , getDaysFromAddPage ,getLabelFromAddPage);
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setCurrentItem(1);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
}
SectionsPagerAdapter.java
public class SectionsPagerAdapter extends FragmentPagerAdapter {
#StringRes
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2 , R.string.tab_text_3};
private final Context mContext;
private String time;
private String format;
private String days;
private String label;
public SectionsPagerAdapter(Context context, FragmentManager fm , String time ,String format , String days , String label ) {
super(fm);
mContext = context;
this.time = time;
this.format = format;
this.days = days;
this.label = label;
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
Fragment fragment = new Fragment();
switch (position)
{
case 0:
fragment = new Edit();
break;
case 1:
fragment = new Alarm();
Bundle bundle = new Bundle();
bundle.putString("HOUR", time);
bundle.putString("FORMAT", format);
bundle.putString("DAYS", days);
bundle.putString("LABEL", label);
fragment.setArguments(bundle);
break;
case 2:
fragment = new Add();
break;
}
return fragment;
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return mContext.getResources().getString(TAB_TITLES[position]);
}
#Override
public int getCount() {
// Show 2 total pages.
return 3;
}
}
And here is Add.class (fragment1)
public class Add extends Fragment {
public Add() {
// Required empty public constructor
}
private TimePicker timePicker ;
private TextView tv ;
private Button add ;
private EditText label;
private String format = "";
public String subject = "";
static final String ADD_TIME_ALARM = "add_alarm";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_add, container, false);
tv = (TextView) v.findViewById(R.id.textView5);
timePicker = (TimePicker) v.findViewById(R.id.datePicker1);
label = (EditText) v.findViewById(R.id.subject) ;
add = (Button) v.findViewById(R.id.addAlarm);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addNewAlarm();
}
});
return v;
}
public void addNewAlarm()
{
subject = label.getText().toString();
int hour = timePicker.getHour();
//int minute = timePicker.getCurrentMinute();
if (hour == 0) {
hour += 12;
format = "AM";
} else if (hour == 12) {
format = "PM";
} else if (hour > 12) {
hour -= 12;
format = "PM";
} else {
format = "AM";
}
Intent intent = new Intent(getActivity().getBaseContext(), MainActivity.class);
intent.putExtra("HOURS_ADD_PAGE" , String.valueOf(hour));
intent.putExtra("FORMAT_ADD_PAGE" , format);
intent.putExtra("DAYS_ADD_PAGE" , "wed , mon");
intent.putExtra("LABEL_ADD_PAGE" , subject);
startActivity(intent);
}
}
Finaly here is Alarm.class(Fragment2)
public class Alarm extends Fragment {
public ArrayList<Times> Alarms = new ArrayList<>();
public Alarm() {
// Required empty public constructor
}
ListView list;
Button goAddAlarm ;
String format = "";
String subject = "";
myAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_alarm, container, false);
list = v.findViewById(R.id.listview);
Alarms.add(new Times( "07:09" , "AM" , "Wed , Mon" , "-Home Time"));
//This Func to add new item from Add.class to Alarm(List View)
adapter = new myAdapter(getContext(), R.layout.custom_list_alarm , Alarms);
list.setAdapter(adapter);
return v;
}
public void addNewAlarm(){
Bundle bundle = getArguments();
String hour = bundle.getString("HOUR");
String format = bundle.getString("FORMAT");
String days = bundle.getString("DAYS");
String label = bundle.getString("LABEL");
Alarms.add(new Times( hour , format , days , label));
}
}
List view Adapter
public class myAdapter extends BaseAdapter {
private Context c;
Fragment fr ;
private int res ;
private ArrayList<Times> time;
public myAdapter() {
}
public myAdapter(Context c , int res , ArrayList<Times> time)
{
this.c = c;
this.res = res;
this.time = time;
}
public void addAlarm(Times times)
{
this.time.add(times);
}
#Override
public int getCount() {
return time.size();
}
#Override
public Times getItem(int position) {
return time.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null)
{
v = LayoutInflater.from(c).inflate(res , null , false);
}
TextView tv_name = v.findViewById(R.id.textView);
TextView tv_name2 = v.findViewById(R.id.textView2);
TextView tv_name3 = v.findViewById(R.id.textView3);
TextView tv_name4 = v.findViewById(R.id.textView4);
Times t = getItem(position);
tv_name.setText(t.getTime());
tv_name3.setText(t.getDay());
tv_name2.setText(t.getDays());
tv_name4.setText(t.getSubject());
return v;
}
}
You should notify your adapter with adapter.notifyDataSetChanged() whenever you're adding new items to the list. Also consider using RecyclerView instead of ListView. It has bigger advantages and improvements from a ListView.
i have a main activity and alot with fragments in it .. now i want to pass data between fragment to another fragment
Help me with this ...
try to use intent but its never did what i want and errors occures
this is my source fragment :
public class SubCategoryDetail extends Fragment {
View view;
SubCategory subCategory;
SwipeRefreshLayout swipeRefreshLayout;
RecyclerView includedView, relatedView, popularView;
TextView titlePricing, titleIncluded, pricing1, pricing2;
Spinner spinner;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.subcategory_detail, container, false);
bindViews();
((HomeActivity) getActivity()).visibility(false, false, false);
return view;
}
private void bindViews() {
((HomeActivity) getActivity()).toolbar.setVisibility(View.VISIBLE);
Bundle bundle = getArguments();
if (bundle != null) {
subCategory = (SubCategory) bundle.getSerializable("data");
}
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
spinner = (Spinner) view.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new ItemSelectedListener());
getDetails();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
getDetails();
}
});
}
Glide.with(getActivity()).load(subCategory.getImage()).into(bannerTop);
title.setText(subCategory.getName());
ImageAdapter imageAdapter = new ImageAdapter(subCategory.getImages(), getActivity());
recyclerView.setAdapter(imageAdapter);
description.setText(subCategory.getDescription());
IncludedAdapter includedAdapter = new IncludedAdapter(subCategory.getInclude(), getActivity());
includedView.setAdapter(includedAdapter);
PopularAdapter popularAdapter = new PopularAdapter(subCategory.getPopular(), getActivity());
popularView.setAdapter(popularAdapter);
TypefaceHelper.typeface(title, MyApplication.getBold());
TypefaceHelper.typeface(description, MyApplication.getLight());
TypefaceHelper.typeface(allProvider, MyApplication.getRegular());
TypefaceHelper.typeface(titleIncluded, MyApplication.getRegular());
TypefaceHelper.typeface(titlePricing, MyApplication.getRegular());
TypefaceHelper.typeface(pricing1, MyApplication.getLight());
TypefaceHelper.typeface(pricing2, MyApplication.getLight());
}
private void disableViews(Boolean what) {
for (int i = 0; i < swipeRefreshLayout.getChildCount(); i++) {
View child = swipeRefreshLayout.getChildAt(i);
child.setEnabled(what);
}
}
public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.Holder> {
private List<Category> list;
private Context context;
public PopularAdapter(List<Category> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public PopularAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new PopularAdapter.Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.popular_item, parent, false));
}
#Override
public void onBindViewHolder(PopularAdapter.Holder holder, int position) {
final Category category = list.get(position);
Glide.with(context).load(category.getImage())
.apply(new RequestOptions()
.placeholder(R.drawable.home)
.error(R.drawable.home)
.centerCrop())
.into(holder.icon);
holder.name.setText(category.getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putSerializable("data", category);
bundle.putString("from", "one");
bundle.putString("name", category.getName());
SubCategoryFragment fragment = new SubCategoryFragment();
fragment.setArguments(bundle);
((HomeActivity) context).changeFragment(fragment, "Sub Category");
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public class Holder extends RecyclerView.ViewHolder {
ImageView icon;
TextView name;
public Holder(View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.category_item);
name = (TextView) itemView.findViewById(R.id.cat_name);
TypefaceHelper.typeface(name, MyApplication.getLight());
}
}
}
Mainly this is what i want to pass !!
public class ItemSelectedListener implements AdapterView.OnItemSelectedListener {
//get strings of first item
String firstItem = String.valueOf(spinner.getSelectedItem());
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(pos == 0) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
}
else if(pos == 1) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
Toast.makeText(parent.getContext(), ex2, Toast.LENGTH_LONG).show();
}
else if(pos == 2) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
Toast.makeText(parent.getContext(), ex2, Toast.LENGTH_LONG).show();
}
else if(pos == 3) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
Toast.makeText(parent.getContext(), ex2, Toast.LENGTH_LONG).show();
}
else if(pos == 4) {
String ex = parent.getItemAtPosition(pos).toString();
String ex2 = ex.substring(ex.lastIndexOf("+") + 1);
Toast.makeText(parent.getContext(), ex2, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg) {
}
}
}
please tell me the way that i can pass this data to a textview in the second fragment
If you want to pass the data to a fragment say myFragment then see snippet below
Bundle bundle=new Bundle();
bundle.putString("message",msg); //here msg is value you want to pass
myFragment fragment=new myFragment();
fragment.setArguments(bundle);`
now to access this in myFragment class using code (generally declared in oncreate() of fragment class):
Bundle bundle=getArguments();
if(bundle!=null){
Textiew textView.setText(String.valueOf(bundle.getString("message")));
}
I am working on an app that includes shopping feature and items are added to cart.
I have navigation drawer. In that, I have my first fragment called as "POSFragment" in which I have implemented a viewpager as data to be shown in the form of swipeable tabs. The data will be inflated dynamically. So, I have used PagerTabStrip and a viewpager. There is a common fragment called "MenuDetailFragment" in which data will be inflated dynamically through a webservice. the adapter for this fragment is "MenuPagerAdapter". Now, In this fragment the data will shown in listview. In Listview in each row items can be incremented or decremented using plus and minus Buttons in each row. Between these button there is textview where the quantity will be displayed correspondingly. I have used a Linkedhashmap on both buttons so, the items along with the quantity is saved in linkedhashmap and then in Sharedpreferences. It's working fine till now. The issue occurs when I swipe in the View pager and next fragment appears and When I click on increment button or decrement button on item there. In shared preferences, It starts saving the current fragment items and previous fragment data is not there anymore. I have already used the "yourcustomviewpager.setOffscreenPageLimit(3)" So, I can see the selected items with the quantities whe I swipe to and fro but the data i stored in sharedpreferences using Linkedhashmap is not there instead current fragment's data is saved. I want that the new data is added to previous data and data in all the fragments should persist. Is there a way to do so? I have been facing this issue for quite a long time.
Here are some screenshots and relevant code snippet.
public class PosFragment extends Fragment {
private ViewPager viewPager;
View rootview;
String code;
String name;
String desc;
String rate;
String key;
Button viewCartBtn;
static TextView displayPrice;
static TextView noOfItemsInCart;
RippleView rippleView;
//private sendPosition pos;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Log.e("oncreateview", "called");
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.pos_fragment_layout, container, false);
initViews();
new AsyncTaskGetMenu().execute();
return rootview;
}
public void initViews() {
Log.e("init", "called");
viewPager = (ViewPager) rootview.findViewById(R.id.viewPager);
Button viewCartButton = (Button) rootview.findViewById(R.id.view_cart_btn);
viewPager.setOffscreenPageLimit(3);
viewCartBtn = (Button) rootview.findViewById(R.id.view_cart_btn);
AppMethods.setGlametrixBoldFont(getActivity(), viewCartBtn);
displayPrice = (TextView) rootview.findViewById(R.id.textView_totalprice_cart);
noOfItemsInCart = (TextView) rootview.findViewById(R.id.cart_items_quantity);
viewCartBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ViewCartActivity.class);
intent.putExtra(AppConstants.SUBTOTAL, Integer.parseInt(displayPrice.getText().toString()));
startActivity(intent);
}
});
LinearLayout itemsInCartLinearLayout = (LinearLayout) rootview.findViewById(R.id.ll_items_in_cart);
itemsInCartLinearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ViewCartActivity.class);
intent.putExtra(AppConstants.SUBTOTAL, Integer.parseInt(displayPrice.getText().toString()));
startActivity(intent);
}
});
}
This is "MenuDetailFragment" class
public class MenuDetailFragment extends Fragment {
View rootview;
private ListView listView;
private ArrayList<MenuHeadingDetailsModel> menuheadingDetailList;
private Communicate comm;
LinkedHashMap<String, ViewCartDetailsModel> viewCartDetailsModelMap = new LinkedHashMap<>();
ViewCartDetailsModel viewCartDetailsModel;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.menu_list, container, false);
InitializeViews();
return rootview;
}
public void InitializeViews() {
listView = (ListView) rootview.findViewById(R.id.menu_items_list);
Bundle bundle = getArguments();
menuheadingDetailList = bundle.getParcelableArrayList(AppConstants.MENU_KEY);
listView.setAdapter(new MenuAdapter(getActivity(), menuheadingDetailList));[enter image description here][1]
This is"MenuPagerAdapter" class
public class MenuPagerAdapter extends FragmentStatePagerAdapter {
List<MenuModel> menu;
public MenuPagerAdapter(FragmentManager fm, List<MenuModel> menuList) {
super(fm);
this.menu = menuList;
}
#Override
public CharSequence getPageTitle(int position) {
return menu.get(position).getHeading().toString();
}
private List<MenuHeadingDetailsModel> getMenuHeadingDetails(int pos) {
return this.menu.get(pos).getMenuHeadingDetailList();
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new MenuDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(AppConstants.MENU_KEY, (ArrayList<? extends Parcelable>) getMenuHeadingDetails(position));
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return menu.size();
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
if (observer == null) {
super.unregisterDataSetObserver(observer);
}
}
#Override
public void destroyItem (ViewGroup container, int position, Object object)
{
Log.e("destroyitem","called");
}
}
class MenuAdapter extends BaseAdapter {
//ViewHolder holder;
Context context;
List<MenuHeadingDetailsModel> myItems = new ArrayList<>();
Button buttonPlus, buttonMinus;
int totalAmount = 0;
int pagePosition;
public MenuAdapter(Context context, List<MenuHeadingDetailsModel> myItems) {
this.context = context;
this.myItems = myItems;
}
#Override
public int getCount() {
return myItems.size();
}
#Override
public Object getItem(int position) {
return myItems.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder = new ViewHolder();
final LayoutInflater inflater = getActivity().getLayoutInflater();
convertView = inflater.inflate(R.layout.menu_list_item, null);
viewHolder.codeText = (TextView) convertView.findViewById(R.id.textView_code);
viewHolder.nameText = (TextView) convertView.findViewById(R.id.textView_name);
AppMethods.setGlametrixBoldFont(getActivity(), viewHolder.nameText);
viewHolder.descText = (TextView) convertView.findViewById(R.id.textView_desc);
viewHolder.rateText = (TextView) convertView.findViewById(R.id.textView_rate);
viewHolder.textViewNoOfItems = (TextView) convertView.findViewById(R.id.tv_no_of_items);
buttonMinus = (Button) convertView.findViewById(R.id.button_minus);
buttonPlus = (Button) convertView.findViewById(R.id.button_plus);
final MenuHeadingDetailsModel menu = myItems.get(position);
viewHolder.codeText.setText(menu.getCode());
viewHolder.nameText.setText(menu.getItemName());
viewHolder.descText.setText(menu.getDescription());
viewHolder.rateText.setText(menu.getRate());
viewHolder.textViewNoOfItems.setText("" + menu.getQuantity());
buttonPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MenuHeadingDetailsModel menuList = myItems.get(position);
menuList.setQuantity(menuList.getQuantity() + 1);
myItems.set(position, menuList);
int itemRate = Integer.parseInt(myItems.get(position).getRate());
int itemQuan = menuList.getQuantity();
String code = menuList.getCode();
String name = menuList.getItemName();
int totalAmountPerItem = itemQuan * itemRate;
viewCartDetailsModel = new ViewCartDetailsModel(code, name, itemQuan, itemRate);
viewCartDetailsModelMap.put(viewCartDetailsModel.getCode(), viewCartDetailsModel);
Log.e("hashmap", "size" + viewCartDetailsModelMap.size());
Gson gson = new Gson();
String hashMapToString = gson.toJson(viewCartDetailsModelMap);
AppPreferences.saveDataInSharedpreferences(getActivity(), AppConstants.VIEW_ITEMS_INT_CART_KEY, hashMapToString);
totalAmount += itemRate;
int amount = totalAmount;
onButtonPressedForAmount(amount);
notifyDataSetChanged();
int quan = 0;
TextView tv;
for (int i = 0; i < listView.getCount(); i++) {
View view = listView.getAdapter().getView(i, null, null);
tv = (TextView) view.findViewById(R.id.tv_no_of_items);
quan = quan + Integer.parseInt(tv.getText().toString());
}
onButtonClickedForQuantity(quan);
}
}
);
buttonMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MenuHeadingDetailsModel menuList = myItems.get(position);
if ((menuList.getQuantity() - 1) >= 0) {
menuList.setQuantity(menuList.getQuantity() - 1);
myItems.set(position, menuList);
int itemRate = Integer.parseInt(myItems.get(position).getRate());
int itemQuan = menuList.getQuantity();
String code = menuList.getCode();
String name = menuList.getItemName();
//int totalAmountPerItem = itemQuan * itemRate;
viewCartDetailsModel = new ViewCartDetailsModel(code, name, itemQuan, itemRate);
if (itemQuan == 0) {
viewCartDetailsModelMap.remove(code);
} else {
viewCartDetailsModelMap.put(viewCartDetailsModel.getCode(), viewCartDetailsModel);
}
Gson gson = new Gson();
String hashMapToString = gson.toJson(viewCartDetailsModelMap);
AppPreferences.saveDataInSharedpreferences(getActivity(), AppConstants.VIEW_ITEMS_INT_CART_KEY, hashMapToString);
if (totalAmount > 0) {
totalAmount -= itemRate;
Log.e("totalamount", "minus" + totalAmount);
int amount = totalAmount;
onButtonPressedForAmount(amount);
}
} else {
menuList.setQuantity(0);
myItems.set(position, menuList);
}
notifyDataSetChanged();
int quan = 0;
TextView tv;
for (int i = 0; i < listView.getCount(); i++) {
View view = listView.getAdapter().getView(i, null, null);
tv = (TextView) view.findViewById(R.id.tv_no_of_items);
quan = quan + Integer.parseInt(tv.getText().toString());
}
onButtonClickedForQuantity(quan);
}
}
);
return convertView;
}
public void onButtonPressedForAmount(int userData) {
if (comm != null) {
comm.sendAmount(userData);
}
}
public void onButtonClickedForQuantity(int quantity) {
if (comm != null) {
comm.sendQuantity(quantity);
}
}
class ViewHolder {
TextView nameText;
TextView codeText;
TextView descText;
TextView rateText;
TextView textViewNoOfItems;
}
}
//For settings fonts to the alert diaolog for customization of items
//public void setGlametrixBoldFont(TextView textView) {
//Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/GlametrixBold.otf");
//textView.setTypeface(typeface);
// }
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
comm = (Communicate) activity;
} catch (Exception e) {
AppMethods.getStackTrace(e);
}
}
public interface Communicate {
public void sendAmount(int amount);
public void sendQuantity(int quantity);
}
// public void getPosition(int Position) {
//
// int pagePosition = Position;
// Log.e("pos", " " + pagePosition);
// }
}
You can get your current fragment inside view pager by following below way
SampleFragment sampleFr = (SampleFragment) viewPager.getAdapter().instantiateItem(viewPager, 0);
Now you can access any data you want using that fragment.
For the first time adding fragments in view pager do like below way.
BaseFragment frTimeLine = (BaseFragment) Fragment.instantiate(this, SampleFragment.class.getName());
fragments.add(frTimeLine);
BaseFragment frEarnTiqs = (BaseFragment) Fragment.instantiate(this, SampleFragment1.class.getName());
fragments.add(frEarnTiqs);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), getResources(), fragments);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
First, I'll preface my question with the fact that I'm not using a CursorLoader.
I'm pulling in data from a SQLlite database to populate a listview in a ListFragment. The initial load works well, but once the data is manipulated (i.e. an addition is made to the list), the listview NEVER refreshes to show the new data. I am implementing the Loader callbacks like so:
public class BillListingFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Bill>> {
private billListAdapter mAdapter;
private static final int LOADER_ID = 1;
private SQLiteDatabase mDatabase;
private BillsDataSource mDataSource;
private BillsStoreDatabaseHelper mDbHelper;
/**
* The fragment argument representing the fragment type (archive or outstanding)
*/
private static final String ARG_FRAGMENT_TYPE = "fragment_type";
/**
* Returns a new instance of this fragment based on type
*/
public static BillListingFragment newInstance(String type) {
// TODO: Make the fragment type an enum
BillListingFragment fragment = new BillListingFragment();
Bundle args = new Bundle();
args.putString(ARG_FRAGMENT_TYPE, type);
fragment.setArguments(args);
return fragment;
}
public BillListingFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bill_view_layout, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mDbHelper = new BillsStoreDatabaseHelper(getActivity());
mDatabase = mDbHelper.getWritableDatabase();
mDataSource = new BillsDataSource(mDatabase);
mAdapter = new billListAdapter(getActivity(), R.layout.bill_row_layout);
setListAdapter(mAdapter);
getLoaderManager().initLoader(LOADER_ID, null, this);
}
#Override
public Loader<List<Bill>> onCreateLoader(int id, Bundle args) {
BillDataLoader loader = new BillDataLoader(getActivity(), mDataSource);
return loader;
}
#Override
public void onLoadFinished(Loader<List<Bill>> loader, List<Bill> data) {
for(Bill bill: data){
mAdapter.add(bill);
}
setListAdapter(mAdapter);
}
#Override
public void onLoaderReset(Loader<List<Bill>> loader) {
mAdapter.clear();
}
#Override
public void onDestroy() {
super.onDestroy();
mDbHelper.close();
mDatabase.close();
mDataSource = null;
mDbHelper = null;
mDatabase = null;
}
public void reload(){
getLoaderManager().restartLoader(LOADER_ID, null, this);
}
private class billListAdapter extends ArrayAdapter<Bill> {
Context context;
public billListAdapter(Context context, int resourceID){
super(context, resourceID);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.bill_row_layout, parent, false);
}
TextView payToField = (TextView) convertView.findViewById(R.id.nameField);
TextView dueDateField = (TextView) convertView.findViewById(R.id.overdueField);
payToField.setText(getItem(position).getPayTo());
// calculate days until due
Bill bill = getItem(position);
// TODO: Add how many days until bill in overdue field + add color
JodaTimeAndroid.init(getActivity());
DateTime dueDateDt = new DateTime(bill.getDateDue());
DateTime currentDt = new DateTime();
int daysDifference = Days.daysBetween(currentDt.toLocalDate(), dueDateDt.toLocalDate()).getDays();
// depending on what that differential looks like set text / color
if (daysDifference > 1) {
dueDateField.setText(Integer.toString(daysDifference) + " Days");
} else {
if (daysDifference == 0) {
dueDateField.setText("DUE TODAY");
} else {
if (daysDifference < 0) {
}
}
}
return convertView;
}
}
}
I have debugged my code so I know that the onLoadFinished callback is being made after the data has been manipulated. I also know that adapter contains the updated data at this point. I have tried resetting the adapter via setListAdapter(mAdatper) and every notifyDataChanged-like method I can find, but to no avail. What is going on here and how can I get the listview to update?
I am developing an android app for students who can take test and exams.Here my question is that I have set of questions say 50 each having 4 answer options in a selectable listview manner.Now,what i want to ask is that i want them to be called in one activity only and not 50 searate activties.
here's the sample code
/***ArrayList goes here*****/
stuff.add("I'm noting.");
stuff.add("I always do nthing.");
stuff.add("All my efforts");
lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,stuff );
lv.setAdapter(arrayAdapter);
Button sbt=(Button)findViewById(R.id.sbt);
sbt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),Screen5cActivity.class);
startActivity(i);
}
});
So,one method for doing this that comes to my mind is that take a static counter and keep on recreating that same activity by inflating data from same arraylist but that method gets a bit obfuscated as for storing result and other.
Any,other solutions withh efficiency and better explaination are welcome.
Okay, well here is how i would do this.
I create a MainActivity as Follows :
public class MainActivity extends FragmentActivity implements QuestionAnswerInterface{
private static final String FRAGMENT_TAG = "QuestionAnswerFragment";
private String [] questionsArray = {"How old was John Wayne when he died ?","Who was the First U.S. President ?", "How many vertices are on a Octagon ?"};
private String [][] answers = {{"43","56","34","none of these"},
{"George Bush","Barrack Obama","George Washington","none of these"},
{"6","4","8","none of these"}
};
private QuestionAnswerFragment fragment = null;
private ArrayList<ArrayList<String>> answersListOfLists = null;
private ArrayList<String> answersList = null;
private ArrayList<String> questionsList = null;
// Ultimate Holder to Link the ArrayLists
private HashMap<ArrayList<String>, ArrayList<ArrayList<String>>> questionAnswerList = null;
#Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// -------- Setup the Questions -------
setupQuestions(questionsArray, answers);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if(fragment == null)
{
// Create the Fragment
fragment = (QuestionAnswerFragment) QuestionAnswerFragment.newInstance(new Bundle(), "Test Example" , questionAnswerList);
ft.add(R.id.frameLayoutTest, fragment, FRAGMENT_TAG);
ft.commit();
}else{
fragment = (QuestionAnswerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
ft.show(fragment);
}
}
private void setupQuestions(String [] questions, String[][] answers)
{
// The Ultimate Wrapper
questionAnswerList = new HashMap<ArrayList<String>, ArrayList<ArrayList<String>>>();
// ArrayList to hold the List of lists of answers
answersListOfLists = new ArrayList<ArrayList<String>>();
// ArrayList to hold the list of Questions
questionsList = new ArrayList<String>();
// Loop Through the Questions
for(int i = 0; i < questionsArray.length ; i++)
{
// Add them to the List
questionsList.add(questions[i]);
}
//*** Magic Stuff ***
for(int l = 0; l < answers.length ; l++)
{
// Needs to be created each time we add a new set of answers
answersList = new ArrayList<String>();
// Loop through the inner array values
for(int m = 0; m < answers[l].length ; m++)
{
// Add the Answers for index l using values of index m
answersList.add(answers[l][m]);
}
answersListOfLists.add(answersList);
}
questionAnswerList.put(questionsList, answersListOfLists);
}
#Override
public void sendBackAnswer(String answer) {
// TODO Auto-generated method stub
}
}
And then My Fragment Class :
public class QuestionAnswerFragment extends Fragment{
private static HashMap<ArrayList<String>, ArrayList<ArrayList<String>>> m_questionAnswerList;
private static String m_textName = null;
private static final String TAG = "QuestionAnswerFragment";
private QuestionAnswerInterface m_callBack = null;
private AnswerAdapter m_adapter = null;
private ArrayList<ArrayList<String>> m_answers = null;
private ArrayList<String> m_questions = null;
private int m_questionCount = 0;
private String currentQuestion = null;
private Entry<ArrayList<String>, ArrayList<ArrayList<String>>> entry = null;
private Iterator<Entry<ArrayList<String>, ArrayList<ArrayList<String>>>> iterator = null;
// UI Elements
private ListView m_listViewAnswers = null;
private Button m_buttonSubmitAnswer = null;
private TextView m_textViewQuestion = null;
// ----------------------------------------------------------------------------
// Interface
public interface QuestionAnswerInterface
{
// Returns the Right or wrong Answer to be kept for score calculation
public abstract void sendBackAnswer(String answer);
}
// Instance Method, so we can share the relevant information with the fragment
public static QuestionAnswerFragment newInstance(Bundle args, String testName, HashMap<ArrayList<String>, ArrayList<ArrayList<String>>> questionAnswerList)
{
QuestionAnswerFragment fragment = new QuestionAnswerFragment();
m_textName = testName;
m_questionAnswerList = questionAnswerList;
fragment.setArguments(args);
return fragment;
}
// --------------------------------------------------------------------------
// Class Overrides
#Override public void onAttach(Activity activity)
{
// Default Behavior
super.onAttach(activity);
try{
// Attach the Interface to the Parent Activity
m_callBack = (QuestionAnswerInterface) activity;
}catch(ClassCastException ex){
// Log the Error
Log.d(TAG, "Failed to Implement Interface in the Parent Activity " + ex.getMessage());
}
}
#Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the Layout from the XML Resource
return inflater.inflate(R.layout.question_answer_fragment, null);
}
#Override public void onActivityCreated(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// Get a Reference to All the Views
m_listViewAnswers = (ListView) getView().findViewById(R.id.listViewAnswerOptions);
m_textViewQuestion = (TextView) getView().findViewById(R.id.textViewQuestion);
m_buttonSubmitAnswer = (Button) getView().findViewById(R.id.buttonDone);
// Add a Listener to the button & ListView
m_buttonSubmitAnswer.setOnClickListener(SubmitListener);
m_listViewAnswers.setOnItemSelectedListener(AnswerListener);
iterator = m_questionAnswerList.entrySet().iterator();
// Start the test from the Beginning using the String [0] as the first question
entry = iterator.next();
m_questions = entry.getKey();
m_answers = entry.getValue();
Log.d("ArraySize Questions", "Size of the Questions Array is "+ m_questions.size());
Log.d("ArraySize Answers", "Size of the Answers Array is "+ m_answers.size());
// Start the Test
updateTest();
}
public void updateTest()
{
m_textViewQuestion.setText(m_questions.get(m_questionCount));
updateAdapter(m_answers.get(m_questionCount));
m_questionCount += 1;
}
private void updateAdapter(ArrayList<String> arrayList)
{
m_adapter = new AnswerAdapter(getActivity(), arrayList);
m_listViewAnswers.setAdapter(m_adapter);
}
private OnItemSelectedListener AnswerListener = new OnItemSelectedListener()
{
#Override public void onItemSelected(AdapterView<?> adapter, View view, int position, long id)
{
// Get the Position of the List Item Selected
// Check if its correct or do what you need to do.
m_callBack.sendBackAnswer(m_listViewAnswers.getSelectedItem().toString());
}
#Override public void onNothingSelected(AdapterView<?> arg0) { }
};
// Submits the Answer to the Parent Activity
private OnClickListener SubmitListener = new OnClickListener()
{
#Override public void onClick(View view)
{
if(m_questionCount != m_questions.size())
{
// Notify the Parent that we want to share the Users choice
updateTest();
}else{
Toast.makeText(getActivity(), "You have reached the End of the Test", Toast.LENGTH_LONG).show();
}
}
};
}
And finally the Adapter Class
public class AnswerAdapter extends BaseAdapter{
//----------------------------------------------------------
// Member Variables
private Context m_classContext = null;
private ArrayList<String> m_answers = null;
private LayoutInflater m_inflater = null;
//----------------------------------------------------------
// Constructor
public AnswerAdapter(Activity activity, ArrayList<String> answers)
{
this.m_classContext = activity;
this.m_answers = answers;
this.m_inflater = LayoutInflater.from(m_classContext);
}
// RowContainer
public class Row
{
TextView m_textAnswer;
CheckBox m_selectedAnswer;
}
// ---------------------------------------------------------
// Class Overrides
#Override public int getCount()
{
int count = 0;
if(m_answers.size() > 0)
{
count = m_answers.size();
}
return count;
}
#Override public Object getItem(int position)
{
// return the Item at the current position
return m_answers.get(position);
}
#Override public long getItemId(int position)
{
// Return the current items position
return position;
}
#Override public View getView(int position, View convertView, ViewGroup parent)
{
Row theRow ;
if(convertView == null){
theRow = new Row();
convertView = m_inflater.inflate(R.layout.answer_row_item, null);
theRow.m_textAnswer = (TextView) convertView.findViewById(R.id.textViewAnswer);
theRow.m_selectedAnswer = (CheckBox) convertView.findViewById(R.id.checkBoxAnswer);
convertView.setTag(theRow);
}else{
theRow = (Row) convertView.getTag();
}
theRow.m_textAnswer.setText(m_answers.get(position).toString());
return convertView;
}
}
So Thats how i would approach this.
Use the Main Activity to Create new Tests, and then use the Interface to Share the results with the parent, so you can calculate totals and scores.
create one Pojo class with variables question.ans1 to ans4
i.e
class Question{
String question;
String ansOne;
String ansTwo;
String ansThree;
String ansFour;
String correctAns;
String selectedAnswer;
}
Now generate an arraylist with 50 questions;
ArrayList list=new ArrayList();
now generate list view with one text view for question and four check boxes for 4 answers.