Search code examples
javaandroidsortingandroid-fragmentsandroid-listfragment

How to Fix sorting issue and data display on fragment in android?


I am learning Android myself. Now, I am trying to learn fragments. Here i would like to host fragment on contact activity.

I have 2 issues

Issue 1: The detail fragment is not showing all data.

for example from my java class .There is new Contact("Ron", "Thal", "+405-315-2827", "ron@ronthal.com")

On list there is Thal which is lastname. Clicking on it Should show Firstname, lastname, phone and email. But it is displaying only first name but with issue 2

Issue 2: I have successfully sorted Lastnames alphabetically on the list fragment. How ever, It is not displaying right data on the detail fragment when i use sorted list on list fragment. The detail fragment is showing fragment data in order i have in my pure java class. It should display related data to list.

When i don't sort, the right data is displayed but with only firstname on detail fragment when clicking lastname on list fragment.

How to fix it? Where i have mistaken?

contact activity

public class ContactActivity extends Activity implements ContactListFragment.ContactListListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);

    }

    @Override
    public void itemClicked(long id){
        //method also defined in the listener

        View fragmentContainer = findViewById(R.id.fragment_detail_container);
        if (fragmentContainer != null){
            ContactDetailsFragment detailsFragment = new ContactDetailsFragment();
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
            detailsFragment.setContact(id);
            fragmentTransaction.replace(R.id.fragment_detail_container, detailsFragment);
            //fragmentTransaction.addToBackStack(null);
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.commit();
        }

    }

}

Contact detail fragment

public class ContactDetailsFragment extends Fragment {
    private long contactId;

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

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null){
            contactId = savedInstanceState.getLong("contactId");
        }
    }


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

        return inflater.inflate(R.layout.fragment_contact_details, container, false);
    }

    @Override
    public void onStart(){
        super.onStart();
        View view = getView();
        if (view != null){

            TextView FNameText = (TextView) view.findViewById(R.id.textFName);
            Contact contact = myContact[(int) contactId];
            FNameText.setText(contact.getFName());
            TextView LNameText = (TextView) view.findViewById(R.id.textLName);
            LNameText.setText(contact.getLName());
            TextView PhoneText = (TextView) view.findViewById(R.id.textPhone);
            PhoneText.setText(contact.getPhone());
            TextView EmailText = (TextView) view.findViewById(R.id.textEmail);
            EmailText.setText(contact.getEmail());

        }
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        savedInstanceState.putLong("contactId", contactId);
    }

    public void setContact(long id){
        this.contactId = id;
    }

}

contact list fragment

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) {

        //String[] lastname = new String[Contact.class]
        //cAdapter = new ArrayAdapter<>(inflater.getContext(), R.layout.fragment_contact_list, myContact);

        String[] lnames = new String[Contact.myContact.length];
        //String[] fnames = new String[Contact.myContact.length];
        for (int i = 0; i < lnames.length; i++){
            lnames[i] = Contact.myContact[i].getLName();
            //fnames[i] = myContact[i].getFName();

        }


        //ArrayAdapter<String> cAdapter;
        //ArrayAdapter<String> cAdapter;

        //cAdapter = new ArrayAdapter<>(inflater.getContext(), android.R.layout.simple_list_item_1, myContact);
        cAdapter = new ArrayAdapter<>(inflater.getContext(), android.R.layout.simple_list_item_1, lnames);
        //to sort alphabetically
        /*
        cAdapter.sort(new Comparator<Contact>() {
            @Override
            public int compare(Contact o1, Contact o2) {
                return o1.toString().compareToIgnoreCase(o2.toString());
            }
        });
        */

        cAdapter.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {

                return o1.compareTo(o2);
            }
        });

        setListAdapter(cAdapter);
        cAdapter.notifyDataSetChanged();

        return super.onCreateView(inflater, container, savedInstanceState);
    }
    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);
        this.listener = (ContactListListener) activity;
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        if (listener != null){
            listener.itemClicked(id);
        }
    }
}

fragment contact detail 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""
        android:id="@+id/textFName"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""
        android:id="@+id/textLName"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""
        android:id="@+id/textPhone"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""
        android:id="@+id/textEmail"
        />
</LinearLayout>

contact activity xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:baselineAligned="false"
    tools:context="edu.uco.rawal.p5rabina.ContactActivity">
        <fragment
            class="edu.uco.rawal.p5rabina.ContactListFragment"
            android:layout_width="0dp"
            android:layout_weight ="1"
            android:layout_height="match_parent"
            android:id="@+id/contact_list_frag"/>


        <FrameLayout
            android:id="@+id/fragment_detail_container"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            >

        </FrameLayout>

</LinearLayout>

Contact.java (Pure java class)

public class Contact {
    private String fname, lname, phone, email;


    public static final Contact[] myContact = {

            new Contact("Rabin", "Awal", "+405-315-0007", "rawal@yahoo.com"),
            new Contact("David", "Gilmour", "+405-315-2027", "david@gilmour.com"),
            new Contact("James", "Hetfield", "+405-315-2527", "james@metallica.com"),
            new Contact("Kirk", "Hammet", "+405-315-2995", "kirk@metallica.com"),
            new Contact("Tom", "Morello", "+405-315-2886", "tom@tommorello.com"),
            new Contact("Angus", "Young", "+405-315-2831", "angus@acdc.com.au"),
            new Contact("Ron", "Thal", "+405-315-2827", "ron@ronthal.com")
    };



    private 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();


        return  fname.toString();
        //return this.fname;

    }


}

Thank You


Solution

  • it is displaying only first name

    In a ListView, that makes sense because an ArrayAdapter<String> will toString your objects, which is return fname.toString(); (which could really just be return fname)

    If you mean inside the detail fragment, then in your XML, each TextView should ideally have android:layout_height="wrap_content" instead of match_parent. Otherwise one TextView occupies the whole screen.

    I have successfully sorted Lastnames alphabetically on the list fragment.

    Okay, then I'm not sure what the problem is... Maybe you meant this was the problem?

    cAdapter.sort(new Comparator<Contact>() {
        @Override
        public int compare(Contact o1, Contact o2) {
            return o1.toString().compareToIgnoreCase(o2.toString());
        }
    });
    

    You have two Contact objects here... If you want to sort by last names, then do that.

    cAdapter.sort(new Comparator<Contact>() {
        @Override
        public int compare(Contact c1, Contact c2) {
            return c1.getLName().compareToIgnoreCase(c2.getLName());
        }
    });
    

    Regarding this code.

       String[] lnames = new String[Contact.myContact.length];
        //String[] fnames = new String[Contact.myContact.length];
        for (int i = 0; i < lnames.length; i++){
            lnames[i] = Contact.myContact[i].getLName();
            //fnames[i] = myContact[i].getFName();
    
        }
    

    I would recommend you try to lookup how to create a ArrayAdapter<Contact> if you want to display all the information of a Contact rather than just the last/first name.

    And I'm also not too sure about using getView() within onStart()... I think you should either use the View from onCreateView or onViewCreated.