ListFragment in Android Studio

Arjun Issar picture Arjun Issar · Mar 18, 2015 · Viewed 12.7k times · Source

I was going through tutorials online trying to understand how a ListFragment works and how do we use it.

The tutorials were a bit vague and I could not understand how it works exactly nor was I able to implement a ListFragment on Android Studio.

Could someone please provide details on how a ListFragment works and how do I implement it on Android Studio.

Also, how do I define a ListView inside a fragment without using ListFragment?

Answer

Daniel Nugent picture Daniel Nugent · Mar 18, 2015

Regarding the last question:

Also, how do I define a ListView inside a fragment without using ListFragment?

I have code that might help.

Here is the MainActivity.java, which includes a Fragment:

    public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new MusicFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent i = new Intent(this, SettingsActivity.class);
            startActivity(i);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class MusicFragment extends Fragment {

        ListView list;
        String[] text = { "House of Whispers","Hot Lunch", "Number of the Beast", "Killers"};
        Integer[] imageId = { R.drawable.hotlunch1, R.drawable.hotlunch2,
                R.drawable.ironmaiden1, R.drawable.ironmaiden2 };

        public MusicFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            list = (ListView) rootView.findViewById(R.id.ListView);
            CustomAdapter adapter = new CustomAdapter(getActivity() , text, imageId );
            list.setAdapter(adapter);

            Log.d("CustomAdapter", "MusicFragment onCreateView successful");

            return rootView;
        }

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


        }

    }
}

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">

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

</RelativeLayout>

Edit: As requested, here is the CustomAdapter:

public class CustomAdapter extends ArrayAdapter<String> {
    private final Activity _context;
    private final String[] _text;
    private final Integer[] _imageId;

    public CustomAdapter(Activity context, String[] text, Integer[] imageId) {
        super(context, R.layout.list_item, text);
        this._context = context;
        this._text = text;
        this._imageId = imageId;
    }

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

        LayoutInflater inflater = _context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_item, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.text);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        txtTitle.setText(_text[position]);
        imageView.setImageResource(_imageId[position]);

        return rowView;
    }

}