I'm currently working on an Android app with two tabbed-fragments. One fragment consists of a pre-populated list in a listview. The other contains a dynamic list to be displayed in a listview.
I want to populate the listview on the second fragment from the list items from the first fragment.
The Problem
1) The ListView on fragment 2 doesn't populate unless the underlying adapter is initialized by a dummy list.
2) Changes on ListView are not instant. If I remove a list item from the list, the item still appears in the ListView on fragment 2. But if I switch between tabs, I see the desired List.
Here's the code:
Fragment 2
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstance) {
// listEntries = new ArrayList<InterimOrderEntry>(0);
listEntries = DummyListData.getOrder();
view = (ListView)inflater.inflate(R.layout.fragment2, container, false);
ListAdapter2 listAdapter2 = new ListAdapter2(orderEntries, getLayoutInflater(savedInstance), container);
view.setAdapter(listAdapter2);
return view;
}
ListAdapter1
private void addToList2(String itemName, int quantity, int itemImageId, Context context){
ListEntry listEntry = new ListEntry(itemName, quantity, itemImageId);
listEntries.clear();
listEntries.addAll(LisAdapter2.getOrderEntries());
listEntries.add(orderEntry);
new ListAdapter2().refreshListEntries(listEntries);
}
ListAdapter2
private static List<ListEntry> listEntries;
public void refreshListEntries(List<ListEntry> newListEntries){
listEntries.clear();
listEntries.addAll(newListEntries);
this.notifyDataSetChanged();
}
Would appreciate any help!
Figured it out, the answer was in the android docs, here's the link:
http://developer.android.com/training/basics/fragments/communicating.html
Related
i got a problem here. As the title said, i want to update a listview in a fragment adding a value from an edittext of another fragment. My main activity is a navigation drawer. My fragment with the listview is the Fragment_hotels and the other fragment witch contains the edittext is the FragmentAddHotel. I want to add in the listview (with the hotels) a new hotel.
Can someone explain how to do that?
Code given below:
Fragment_hotels:
public class Fragment_hotels extends Fragment {
public static boolean flag=false;
public void Add(){
Target.add("Hotel");
Target.add("Hotel1");
Target.add("Hotel2");
}
static ArrayList<String> Target = new ArrayList<String>();
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_hotels, container,false);
ListView listView = (ListView) view.findViewById(R.id.list);
if (flag==false){
this.Add();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),R.layout.mytextview,R.id.tv, Target);
adapter.setNotifyOnChange(true);
// Assign adapter to ListView
listView.setAdapter(adapter);
return view;
}
}
And FragmentAddHotel:
public class FragmentAddHotel extends Fragment {
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_add_hotel, container, false);
return rootView;
}
}
I got an edittext and a button on my FragmentAddHotel layout.
How can i get the value from edittext and add it on the listview?
Sorry if a similar question has already been answered but i am junior programmer.
Tnx in advance :)
To update view in class from another one you can use EventBus library here is the link
Just register you first class by adding:
EventBus.getDefault().register(this); //in your listener class that contains the listview at onCreate method.
Also implement your receiver method:
public void onEventMainThread(String hotelName) {
// Add the hotelName in your Target array list and then notify your adapter by adapter.notifyDataSetChanged();
}
To send data from your fragment just use :
EventBus.getDefault().post("edittextvalue");
Also don't forget to call :
EventBus.getDefault().unregister(this); // In your listener class onDestroy() method.
In YourActivity:
Fragment_hotels frag_hotels;
void addItem(String item){
frag_hotels.addItemToList(item);
}
Fragment_hotels:
List<String> hotelList;
ArrayAdapter<String> adapter;
void addItemToList(String item){
hotelList.add(item);
adapter.notifyDataSetChanged();
}
In FragmentAddHotel:
// you should call whenever adding is need in fragment
((YourActivity)getActivity()).addItem("Your Item");
Through YourActivity, you can communicate each fragment.
Inside the dialogFragment I have viewPager with two pages. Every page contains a custom adapter. One adapter with list of spinners, other adapter with list of EditTexts. ViewPager adds adapters fine.
public class PageFragment extends Fragment {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pagefragment_newprod, null);
LinearLayout ll=(LinearLayout) view.findViewById(R.id.tvLL);
ListView listView=new ListView(getActivity());
ll.addView(listView);
if (pageNumber==0){
dropDownAdapter=new DropDownAdapter(getActivity(), fillListAdapter);
listView.setAdapter(dropDownAdapter);
} else if (pageNumber==1){
boxAdapter = new BoxAdapter(getActivity(), filledFields);
listView.setAdapter(boxAdapter);
}
return view;
}
}
But it works to slow! Current Adapter (I mean at the curren page) create views every milisecond! Look at this:
public class BoxAdapter extends BaseAdapter{
...
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (view == null) {
view = lInflater.inflate(R.layout.addproduct_item, parent, false);
}
Log.d(LOG_TAG, "====As I said every milisecond...======");
EditText editText = (EditText) view.findViewById(R.id.addProductEditText);
editText.setText(p.value);
return view;
}
}
Even when I focused the EditText this Log.d write messages every milisecond!
Besides that, adapter at the next page works too. I have other Log.d at the other adapter getView and it works when I used different page's adapter!
Please help me to understand what is wrong...(
The question has already been solved in the comments. The solution for the OP was apparently to remove complex fragments and their adapter. However, I also had complex fragments in a tab layout with a ViewPager, and the following solution fixed the slow paging problem:
viewPager.setOffscreenPageLimit(2);
The 2 will keep two pages away from the current page in memory. This was enough for me because I had three tabs. Be careful about keeping too many pages in memory, though. See the documentation.
I am beginner in android.
I was having a requirement similar to what is discussed here:
Separate Back Stack for each tab in Android using Fragments.
Now, I am using that project given here gitHub
What I want is to add listview to a fragment..Further on clicking a particular item in list another fragment is visible.
But I am facing a problem in doing it.
For eg here,I get error in using setListAdapter(adapter) function.
Plz help.
public class AppTabAFirstFragment extends BaseFragment {
private Button mGotoButton;
//private String[] characters= {"shs","sds","sdss","sdsd"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_tab_a_first_screen, container, false);
mGotoButton = (Button) view.findViewById(R.id.id_next_tab_a_button);
mGotoButton.setOnClickListener(listener);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(),R.layout.app_tab_d_first_screen, characters);
// setListAdapter(adapter);
return view;
}
In this way,it doesn't allow us to use setListAdapter() function reason being I can't extend ListActivity. I am all confused.
Kindly help.
I haven't checked your links, but as I can see from the little code you posted I think this might be the problem:
You need to lookup your listview from the xml file (if you have defined it there) and set the adapter to that object. For instance:
ListView listView = (ListView) view.findViewById(R.id.my_list_view);
listView.setAdapter(adapter);
I am new in programming to android now my work is paused as I am not getting how to accomplish the following I want to create a view which will have paging capability and in the view there would be textview to display the name of person and a list of name of all his relatives .... And the list can be long so it should be scrollable... I know to do paging and list independently but do not know how to combine the both in one view.... I hope I am clear with my question... So guys help me how to accomplish this...
I know to do paging and list independently but do not know how to combine the both in one view
So you can create xml like this :
Linear Layout/ Relative layout
Text View
List View
Put this XML in fragment
EDIT :
public class Fragment1test extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//This layout contains your list view
View view = inflater.inflate(R.layout.your_view_xml, container, false);
//now you must initialize your list view
Listview listview =(Listview)view.findViewById(R.id.your_listview);
//Here items must be a List<Items> according to your class instead of String[] array
ListAdapter listadapter = new ListAdapter(getActivity(), android.R.layout.simple_list_item_1, items)
ListView.setAdapter( listAdapter());
//getActivty is used instead of Context
return view;
}
}
I have a weird situation with a custom ArrayAdapter.
When I try to update the adpater with new data, instead of the data being updated, the new data are inserted to the beginning of the listview and the old data are remaining and visible once you scroll the listview.
UPDATE
It seems that the problem is caused by the ArrayList from the fragment bundle.
If I don't set the listview in the onCreateView from the fragment bundle, my update code works fine, but now I'm puzzled why this:
ArrayList<Collection> cityStoresList = fragmentBundle.getParcelableArrayList("stores");
mStoresList.addAll(cityStoresList);
is causing the items to always remain on the list?
END OF UPDATE
Here are parts of the code: (Collection is a custom object model class)
ArrayList<Collection> mStoresList = new ArrayList<Collection>();
/** List Adapter */
private StoresListAdapter mListAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
boolean attach = false;
if (container == null) {
attach = true;
}
Bundle fragmentBundle = getArguments();
ArrayList<Collection> cityStoresList = fragmentBundle.getParcelableArrayList("stores");
mStoresList.addAll(cityStoresList);
//inflater code not added here, but is present
mListAdapter = new StoresListAdapter(getActivity(), mStoresList);
mListView.setAdapter(mListAdapter);
return layout;
}
My custom adapter is as follows:
public class StoresListAdapter extends ArrayAdapter<Collection> {
public StoresListAdapter(Context c, ArrayList<Collection> array) {
super(c, 0, array);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// View from recycle
View row = convertView;
// Handle inflation
if (row == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_store, null);
}
// Get the Store
Collection store = getItem(position);
//rest of code follows
return row;
}
}
Now when I want to update my adapter I use the following:
public void updateAdapter(ArrayList<Collection> storesList, final int listIndex) {
mStoresList.clear();
mStoresList.addAll(storesList);
mListAdapter.notifyDataSetChanged();
}
And this creates the issue I mentioned. The new items appear fine, but the previous ones are still visible and added after the new ones.
It's like adding the new items in the ArrayList as the first items, instead of just replacing the old ones.
Any ideas, suggestions?
Ok, finally found the problem.
Because the whole thing is within a fragment, the oncreateView is actually called when I'm attaching the array, so what happens is that my updateAdapter method is called, the items are added and displayed, before the view is actually visible.
Then the oncreateView method is fired and the original bundle items are being added to the Arraylist....