Tabs with Listview in fragments - android

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);

Related

Updating the ListFragment

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_schedule, container, false);
// array adapter + list fragment
arrayAdapter = new ArrayAdapter<>(getActivity(),
R.layout.simple_list_item_1_custom,
schedules.get(getArguments().getInt(ARG_SECTION_NUMBER) - 1)); //
arrayAdapter.setNotifyOnChange(true);
setListAdapter(arrayAdapter);
return rootView;
}
This is the onCreateView of my class that extends ListFragment, I am trying to update the contents of the list from my activity, so far I've tried clearing the arrayAdapter and calling notifyDataSetChanged on it, and it doesn't work. The only way I can get my list to update is if I swipe two tabs and go back again, then it shows all the changed values. Any suggestions?
This is the onCreateView of your fragment. This is fired every time when you create your fragment or swipe between tabs. As I understand, you just add values to the db onClick and when you go and come back your fragment you see the new values.
Please, try adding new values to your adapter on onClick button then try updating your list in the same scope with the NotifyDataChanged mechanisim of the adapter.
If I get wrong idea about whay you are doing pleas share more code.

Why my ViewPager is so slow?

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.

combining paging and list of android in one view

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;
}
}

setText in a ListFragment

I'm a beginner at Android programming and I was wondering if you could help me out.
I have a ListFragment:
public class AdvertFrag extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
that I'm populating from a database using a loaderManager. This works fine and displays details of adverts, but I would like to do some calculations (work out distance via longitude and latitude) and then have the distance to the shops location displayed on each AdvertFrag.
I have looked into using setText() on a TextView in a fragment and have read you should allow the onCreateView method to be called first so a view is present. I have tried the below code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
view = inflater.inflate(R.layout.row, container, false);
if(view == null)
{
Log.i("TEST","null");
}
else
{
distance = (TextView) view.findViewById(R.id.distance);
distance.setText("the distance will go here");
}
return view;
}
But get the error that "Your content must have a ListView whose id attribute is android.R.id.list. Allowing the OnCreateView to just call:
return super.onCreateView(inflater, container, savedInstanceState);
works fine.
So my question is, what am I doing wrong? and how do I get setText working in a ListActivity? Or am i going at it in totally the wrong way?
Addition info if its useful:
My row.xml, just has the text views that are populated by the loader and the additional textview for the distance.
I have a fragmentActivity that has the fragment in its XML, like so:
<fragment
...
android:name="com.example.goclub.AdvertFrag"
.../>
Like user113215 said onCreateView() of the ListFragment is not a place to change widgets inside the list items. You will have to create a custom CursorAdapter for that assuming that you are using one. You can find a nice tutorial about this here. Basically it extends a SimpleCursorAdapter and makes changes in the bindView method.

Android ListView in Fragment

I am trying to create a list of items which contains an image and some description for the image in each individual. After which, the list will be place in a fragment inside the application. Can anyone guide me to create it? I am not too sure how can I do it without the ListActivity.
It seems like your Fragment should subclass ListFragment (not ListActivity). onCreateView() from ListFragment will return a ListView you can then populate.
Here's more information on populating lists from the developer guide: Hello, ListView
Here's a code snippet to help you display ListView in a fragment.:
public class MessagesFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_messages, container,
false);
String[] values = new String[] { "Message1", "Message2", "Message3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
return rootView;
}
}
And, this is how the xml would look like:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
If you do not use 'list' as ID or do not include a ListView into your layout, the application crashes once you try to display the activity or the fragment.
Reference: http://www.vogella.com/tutorials/AndroidListView/article.html#listfragments

Categories

Resources