Listfragment Does Not Accept My Layout

ListFragment does not accept my layout

This sounds like an issue that's been reported to Google already.

Google Issue

If this is indeed your issue, there's a couple of suggestions at the bottom of that page for fixing it (well, more making it work than fixing it).

How can style the ListView of a ListFragment

Just replace the original ListView with your CustomListView Layout within the onCreateView(). Worked for me.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = super.onCreateView(inflater, container,
savedInstanceState);
ListView lv = (ListView) layout.findViewById(android.R.id.list);
ViewGroup parent = (ViewGroup) lv.getParent();

// Remove ListView and add CustomView in its place
int lvIndex = parent.indexOfChild(lv);
parent.removeViewAt(lvIndex);
LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
R.layout.custom_list, container, false);
parent.addView(mLinearLayout, lvIndex, lv.getLayoutParams());
return layout;
}

List Fragment doesn't appear in my layout

The problem was in this line:

public ListaCategoriasAdapter(Activity context) {
super(context, R.layout.itemlistacategoriasfragment);

I didn't call good the constructor because I didn't send it the data, now It works:

public ListaCategoriasAdapter(Activity context) {
super(context, R.layout.itemlistacategoriasfragment, month);

ListFragment Not Rendering and getView() in Adapter Not Being Called

I think I found the problem. The issue appears because of the ShackGestureListener that you setup at the start of the onActivityCreated method in the FragmentTopicView:

final ShackGestureListener listener = Helper.setGestureEnabledContentView(R.layout.topics, getActivity());
if (listener != null) {
listener.addListener(this);
}

In the setGestureEnabledContentView() method you check to see if the user enabled the gestures in the preferences or if the android version is bigger then 3. Either way, true or false you set the content view for the FragmentActivityTopic again(with the layout of the FragmentTopicView). Setting the content view again will, unfortunately, cover the current layout which holds the ListView with data(ListView that populates with no problems). When you run those AsyncTasks to get the data, at the end you set the data on the correct ListView(returned by getListView) because the getListView will hold a reference to the old correct ListView which was set in the onCreateView method, but you don't see anything because in the setGestureEnabledContentView you cover this ListView.

This behavior is easy to see if you simple comment out(or remove) the lines that set the content view for the activity in the Helper and HelperAPI4 classes. Another way to see that your ListView is covered is, for example to set the adapter for the ListView(tva) using getListView and using the getActivity().findViewById(android.R.id.list)(I've done this on selecting one of your menus items, so I can control when I replace the adapter):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId())
{
case R.id.topic_menu_newpost: // Launch post form
//this doesn't work as the getListView will return a reference to the old ListView
ListView lv = getListView();
lv.setAdapter(tva);
// this will work as you get a reference to the new ListView on top
ListView lv = (ListView) getActivity().findViewById(android.R.id.list);
lv.setAdapter(tva);

I don't know what to recommend as a solution as I don't quite understand what you're doing, but I doubt that it requires to set the content view for the activity again(and you should work from this).

ListFragment with custom layout doesn't populate empty list notifier

I had simply forgot to set the text!

After setting it, my TextView is displayed correctly:

<TextView android:id="@id/android:empty"
android:textSize="16sp"
android:textStyle="bold"
android:text="@string/empty_text"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

with string resource in strings.xml file:

<string name="empty_text">There are no data</string>

Unfortunately, the TextView is also displayed in each row of the list, but this is another question.

Onclicklistner not working in fragment listview

finally solved the issue when all the controls(Buttons,textviews) in listview set to focusable false.

Items in ListFragment would not show in Listview using Custom Array Adapter

Well there are a few issues you might have with the fragment lifecycle due to the statics in your MyJobsActivity, but as for showing the data itself:

In your MyJobsActivity.onActivityCreated (or wherever you end up associating your custom adapter with the underlying ListFragment), you need to use setListAdapter instead of setAdapter.

From the ListFragment docs (see Binding to Data):

You must use ListFragment.setListAdapter() to associate the list with an adapter. Do not directly call ListView.setAdapter() or else important initialization will be skipped.

To be a bit more clear, one of the many benefits of using/extending a ListFragment is that it will internally handle the binding between your custom adapter and the underlying ListView, but the adapter must be added to the ListFragment itself (i.e., setListAdapter), not on the ListView.

A few other observations:

In fact, unless you need a custom layout, I would recommend completely removing your implementation of onCreateView. This will allow the ListFragment to use its default layout, initialization, and handling for its internal ListView and you'll get (default) handling of an empty list, a loading progress bar, etc. as well.

Given the code you've posted, I would recommend against using a private ListView member in your MyJobsActivity - its parent ListFragment already maintains a copy of the underlying ListView for you, and if you're properly setting the adapter via setListAdapter, you don't need the explicit object (and if you do, use ListFragment's getListView()).

Regardless, if my own trials and challenges are any indication, the static ListView and ArrayList members will most certainly cause you grief when the fragment is destroyed and recreated (on orientation change, on restore of your app from the Recents list if the process has been torn down in the interim, etc.).



Related Topics



Leave a reply



Submit