NumenorForLife
NumenorForLife

Reputation: 1746

ListView in Fragment Not Displayed

I have an application where there are several tabs; interacting with any one calls upon a fragment to be displayed. Unfortunately when I switch to the below fragment, my listView does not appear, despite the fact that the list in question is populated. Thank you very much for any help you can provide.

The fragment's relevant code:

public class Fragment_1 extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    //If time permits, I will try to make a Custom Adapter implemented with a TreeSet
    TreeSet<BlacklistWord> theSet =  MainActivity.getInstance().datasource.GetAllWords();
    ArrayList<String> list = new ArrayList<String>();
    for(BlacklistWord i :theSet){
        System.out.println(i.getWord());
        list.add(i.getWord());
    }
    Collections.sort(list);

    //Making BlackList 
    listView = new ListView(getActivity());
    listView.findViewById(R.id.listview);
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  list);
    listView.setAdapter(adapter);
    ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
    container.addView(listView);
    return inflater.inflate(R.layout.blacklist, container, false);
    //      return inflater.inflate(R.layout.blacklist, container, false);
}
}

The XML is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

Upvotes: 4

Views: 6927

Answers (4)

mkiziltay
mkiziltay

Reputation: 53

ListView visibilty or showing ERROR... notice that if you use fragment which extends ListFragment, use listview defined id like list and define it just like this : android:id="@+id/listview.

and define it in your Fragment Class just like that : frgView.findViewById(android.R.id.listview);

If you still have a problem with visibility of the listview remember that change layout of fragment with <FragmentLayout>.

Would you like use in your project add it under .

Have good coding days...

Upvotes: 0

gunar
gunar

Reputation: 14710

Change the code to (blind coding):

public class Fragment_1 extends SherlockFragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    /**
    ** Change the way you get the data. Don't keep references to activities like that.
    **/
    TreeSet<BlacklistWord> theSet =  MainActivity.getInstance().datasource.GetAllWords();
    ArrayList<String> list = new ArrayList<String>();
    for(BlacklistWord i :theSet){
        System.out.println(i.getWord());
        list.add(i.getWord());
    }
    Collections.sort(list);
    View v = inflater.inflate(R.layout.blacklist, container, false);

    listView = view.findViewById(R.id.listview);
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  list);
    listView.setAdapter(adapter);
    return view;
}
}

Your list view is not showing because you're creating erroneously a ListView with constructor, then you're calling a findViewById (which does nothing useful), then setting an adapter, call notify data set changed and in the end you're returning another list.

Upvotes: 2

Stefan de Bruijn
Stefan de Bruijn

Reputation: 6319

Because you're creating a new ListView, and trying to find your xml listview inside that as a child...

Instead use something as;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View view = inflater.inflate(R.layout.blacklist, container, false);

    ListView listview = (ListView) view.findViewById(R.id.listview);

    //Making BlackList 
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  list);
    listView.setAdapter(adapter);
    ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
    container.addView(listView);

    return view;
}

Upvotes: 1

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

use this way:

public class LListFragment extends ListFragment {

    private String[] line;

    public static final String[] TITLES = { "Henry IV (1)", "Henry V",
            "Henry VIII", "Richard II", "Richard III", "Merchant of Venice",
            "Othello", "King Lear" };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        ListView listv = getListView();

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.scan_row, R.id.textView1, TITLES));
    }
}

or you have listview inside xml then..

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.llist_layout, container, false);
        // do your view initialization here
        listv = (ListView) view.findViewById(R.id.lineDlist);
        name = (TextView) view.findViewById(R.id.lineName);
        st = (TextView) view.findViewById(R.id.lineSt);


        listv.setAdapter(new ImageAdapter(getActivity(),
                GeneralClass.lineDetails));

        return view;
    }

Upvotes: 2

Related Questions