user3207899
user3207899

Reputation: 31

Android - ListFragment and Custom Adapter

im new in Android development. I have a problem using ListFragment and custom Adapter. My list is not populated by objects. Can you check my code please? If I scroll to the bottom, you will see an object, but then crashes.

ListFragmen.java

public class Fragment_List extends ListFragment {

    View item_view ;
    private List<News> arNews = new ArrayList<News>();
     Activity activity =    getActivity();
     private ArrayAdapter<News> arrayAdapter;
     private ListView mListView;

     public void onCreate(Bundle savedInstanceState) 
     {
         super.onCreate(savedInstanceState);

         arrayAdapter = new MyListAdapter();
     }
     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

         // Inflate the layout for this fragment
         item_view= inflater.inflate(R.layout.item_view,null);

         View v1=inflater.inflate(R.layout.fragment_list, container, false);
         mListView= (ListView) v1.findViewById(android.R.id.list);

        populateNewsList();

         return v1;
     }


     public Fragment_List() {

    }

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


        mListView.setAdapter(arrayAdapter);

     }



     private void populateNewsList() {

         arNews.add(new News("Ciao",R.drawable.figura_0icona,"uno"));
        arNews.add(new News("pippo",R.drawable.figura_1icona,"due"));
        arNews.add(new News("pluto",R.drawable.figura_2icona,"tre"));
        arNews.add(new News("Ciao",R.drawable.figura_3icona,"quattro"));
        arNews.add(new News("pippo",R.drawable.figura_4icona,"cinque"));
        arNews.add(new News("pluto",R.drawable.figura_5icona,"sei"));
        arNews.add(new News("Ciao",R.drawable.figura_6icona,"sette"));
        arNews.add(new News("pippo",R.drawable.figura_7icona,"otto"));
        arNews.add(new News("pluto",R.drawable.figura_8icona,"nove"));


    // TODO Auto-generated method stub

     }



        private class MyListAdapter extends ArrayAdapter<News>
        {


            public MyListAdapter(){

                super(getActivity(),R.layout.item_view,arNews);
                Toast.makeText(getContext(), "ciao"+arNews.size(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                //make sure have a view to work with (may have given null)
                View itemView = convertView;

                if(itemView==null)
                {
                    itemView=item_view;
                    if(itemView==null)
                        Toast.makeText(getContext(), "null", Toast.LENGTH_SHORT).show();
                }

                //find news to work with
                News currentNews = arNews.get(position);



                // fill the view
                ImageView imageView = (ImageView) itemView.findViewById(R.id.news_icon);
                imageView.setImageResource(currentNews.getFotoId());


                //make
                TextView makeText = (TextView) itemView.findViewById(R.id.news_anteprima);
                makeText.setText(currentNews.getAntemprima());

                Toast.makeText(getContext(), "testo"+currentNews.getAntemprima(), Toast.LENGTH_SHORT).show();

                return itemView;
            }



        }
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            // TODO Auto-generated method stub
            News clickedNews = arNews.get(position);
            String message = "You clicked position" + position + " anteprima :" + clickedNews.getAntemprima();

            Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();

            super.onListItemClick(l, v, position, id);
        }



}

fragment_list.xml

<?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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="600dp"
        android:layout_height="426dp" >
    </ListView>

</LinearLayout>

Thank you very much!

Upvotes: 1

Views: 499

Answers (1)

Juan Casta&#241;o
Juan Casta&#241;o

Reputation: 120

First of all when you use a ListFragment you need declare two elements on layout (fragment_list.xml):


  1. TextView with id @android:id/empty
    • Used by android when Adapter is empty showing a text


  1. ListView with id @android:id/list
    • Used by android when Adapter is full generating ListView

This ids are in android core then android work implicitily over this, adding, refreshing and deleting elements.

Looking your code you are declaring an object for ListView. This is wrong.

Remove declarations for this objects.

Second of all, your Adapter. You are extends from ArrayList, on your constructor need call super constructor given a Context, a resource, a List. Android Developer Reference: ArrayAdapter. ArrayAdapter(Context context, int resource, T[] objects) By default int resource is 0 (zero).

Summarizing

  1. Add to your layout.xml two items with id.

    • TextView with id @android:id/empty
    • ListView with id @android:id/list
  2. On your Fragment_List class remove all instantiations of ListView.

  3. On your Adapter call super constructor given Context, resource, element List.

  4. Call from your Fragment_List class a new constructor. Android Developer Reference: ListFragment. Say you need to call ListFragment.setListAdapter() against ListFragment.setAdapter().

  5. Try out! I think thats work!

I hope that it helps you.!

Upvotes: 1

Related Questions