robinleathal
robinleathal

Reputation: 251

How to create a list dialog using Dialogfragment and ArrayAdapter to populate items from Java class?

I am learning DialogFragments myself. I have never tried anything than passing intent from a Button, so I am not getting to right direction.

I want to learn all the possible ways.

Is it different than creating a list with ListFragment?

There are 2 buttons on main Activity. On clicking the second Button, it should open a DialogFragment with a list of items, clicking on a list item should open a URL in the browser.

Main Activity

btnDepartment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(MainActivity.this, DepartmentActivity.class);
                //startActivity(intent);

            }

        });

Here is Department Class, which is a pure Java class

public class Department {
    private String deptName, deptUrl;


    public static final Department[] myDepartment = {

            new Department("CS", "http://cs.com"),
            new Department("Biology", "http://bio.com"),
            new Department("Chemistry", "http://www.chemistry.com"),
            new Department("Nursing", "http://nursing.com")
    };

    private Department(String deptName, String deptUrl){
        this.deptName = deptName;
        this.deptUrl = deptUrl;

    }

    public String getDeptName() {
        return deptName;
    }

    public String getDeptUrl() {
        return deptUrl;
    }

    @Override
    public String toString() {
        return deptName;
    }

}

Department Fragment

public class DepartmentFragment extends DialogFragment {

    public DepartmentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_department, container, false);
    }

}

Upvotes: 2

Views: 5115

Answers (2)

Alejandro Traver
Alejandro Traver

Reputation: 79

First, your model must implements Parcelable:

public class Department implements Parcelable {
    private String deptName, deptUrl;


    public static final Department[] myDepartment = {

            new Department("CS", "http://cs.com"),
            new Department("Biology", "http://bio.com"),
            new Department("Chemistry", "http://www.chemistry.com"),
            new Department("Nursing", "http://nursing.com")
    };

    private Department(String deptName, String deptUrl){
        this.deptName = deptName;
        this.deptUrl = deptUrl;

    }

    protected Department(Parcel in) {
        deptName = in.readString();
        deptUrl = in.readString();
    }

    public static final Creator<Department> CREATOR = new Creator<Department>() {
        @Override
        public Department createFromParcel(Parcel in) {
            return new Department(in);
        }

        @Override
        public Department[] newArray(int size) {
            return new Department[size];
        }
    };

    public String getDeptName() {
        return deptName;
    }

    public String getDeptUrl() {
        return deptUrl;
    }

    @Override
    public String toString() {
        return deptName;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(deptName);
        parcel.writeString(deptUrl);
    }
}

After, you must implement the method:

btnDepartment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DepartmentFragment departmentFragment = DepartmentFragment.newInstance(department);
                departmentFragment.show(getSupportFragmentManager(), "departmentFragment");
            }

        });

And the fragment as follows:

public class DepartmentFragment extends DialogFragment {

    private Department department;

    public DepartmentFragment() {
        // Required empty public constructor
    }

    public static DepartmentFragment newInstance(Department department) {
        DepartmentFragment f = new DepartmentFragment();

        // Supply num input as an argument.
        Bundle extras = new Bundle();
        args.putParcelable("department", department);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        department = getArguments().getParcelable("department")
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_department, container, false);
    }

}

Also, you can check all information at http://developer.android.com/reference/android/app/DialogFragment.html

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191874

Is [creating a DialogFragment] different than creating list with ListFragment

Simply: Yes.

Why? Because ListFragment gives you access to both getListView() and setAdapter() and instance methods of the class.

In order to do that with any other Fragment class that does not extend from ListFragment, you must declare some XML layout with a ListView and do something like this

I'll assume fragment_department.xml contains a ListView element of @+id/list and that you do not care about how the Department information is displayed.

public class DepartmentFragment extends DialogFragment {

    public DepartmentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_department, container, false);

        ListView lstView = (ListView) rootView.findViewById(R.id.list);

        ArrayAdapter<Department> adapter = new ArrayAdapter<Department>(getActivity(), android.R.layout.simple_list_item_1, Department.myDepartment);
        lstView.setAdapter(adapter);

        // TODO: adapter.setOnItemClickListener 
        //    ... handle click
        //    ... load webpage

        return rootView;
    }

}

Upvotes: 1

Related Questions