Search code examples
javaandroidlistviewandroid-fragmentsandroid-listfragment

Is it possible to use ArrayAdapter and ListView to create fragments?


I am learning Android myself. I have knowledge on using listView using Arrayadapter. Now I am heading to learn Fragments.

Is it possible to create fragments using ArrayAdapter and listView? Here first fragment on the list should be people name clicking on which the fragment on the right appears with detail contact details of that person.On clicking that detailed contact address it opens email app to send email to that contact.

I have this question because when i tried to add ListView on the xml for the fragment it showing errors that says invalid attributes used Contact Activity (this is activity to host fragments)

public class ContactActivity extends Activity {
    ListView listView;
    //TextView textView;
    ArrayAdapter<Contact> cAdapter;
    private Contact[] myContact = {
            new Contact("Rabin", "Awal", "+405-315-2027", "rawal@yahoo.com"),
            new Contact("David", "Gilmour", "+455-315-2827", "david@gilmour.com"),
            new Contact("James", "Hetfield", "+455-315-0026", "james@metallica.com"),
            new Contact("Kirk", "Hammet", "+445-315-2227", "kirk@metallica.com"),
            new Contact("Tom", "Morello", "+415-315-2497", "tom@tommorello.com"),
            new Contact("Ron", "Thal", "+405-315-2007", "ron@ronthal.com")

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
        //listView = (ListView) findViewById(R.id.contact_list_fragment_container);
        //textView = (TextView) findViewById(R.id.contact_frag_list)
        cAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.contact_list, myContact);
        listView.setAdapter(cAdapter);



    }

Pure Java Class

public class Contact {

    private String fname, lname, phone, email;

    public Contact(String fname, String lname, String phone, String email){
        this.fname = fname;
        this.lname = lname;
        this.email = email;
        this.phone = phone;
    }

    public String getFname() {
        return fname;
    }

    public String getLname() {
        return lname;
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}

contact activity's xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_contact"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="edu.uco.rawal.p5rabina.ContactActivity">

    <FrameLayout
        android:id="@+id/contact_list_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <FrameLayout
        android:id="@+id/contact_detail_fragment_container"
        android:layout_width="0dp"
        android:layout_height="match_parent" />



</RelativeLayout>

Solution

  • Yes in this case there is ListFragment that can be used. Here is how to do

    public class ContactListFragment extends ListFragment {
    
        ArrayAdapter<Contact> cAdapter;
        //ArrayAdapter<String> cAdapter;
    
    
        interface ContactListListener{
            void itemClicked(long id);
        }
        //add listener to fragment
        private ContactListListener listener;
    
        public ContactListFragment() {
            // Required empty public constructor
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
        cAdapter = new ArrayAdapter<>(inflater.getContext(), simple_list_item_1, myContact);  
    .....
    setListAdapter(cAdapter);
            cAdapter.notifyDataSetChanged();
    
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    
    }
    

    them in activity we need to implement it

    public class ContactActivity extends Activity implements ContactListFragment.ContactListListener{
    
         @Override
        public void itemClicked(long id){
            //method also defined in the listener
    
            View fragmentContainer = findViewById(R.id.fragment_detail_container);
       //here instanciate the fragment and commit
    
    
        }
    
    }