LoveAndroid
LoveAndroid

Reputation: 186

sending the arraylist through the interface gives class cast exception?

I am sending the arraylist from one fragment to the another by using the interface.but when i make the instance of the interface it gives class cast exception.can anyone tell me what i am doing wrong ??

This is my interface:-

public class SaleFragment extends UpdatableFragment  {

  private SendArrayList mSendArrayList;

  public interface SendArrayList {

            public void sendData(ArrayList<String> listCustomer);

        }
    }

And this is the code where i am making the instance of the interface and add the arraylist to it :-

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

        mSendArrayList = (SendArrayList)getActivity();

        dbh = new DatabaseHandler(getContext());

        Cursor cursor = dbh.getAllCustomer();
        if (cursor.moveToFirst()) {
            do {
                String customerName = cursor.getString(0);
                mListCustomerName.add(customerName);

            } while (cursor.moveToNext());
        }

        mSendArrayList.sendData(mListCustomerName);



    }

at this line " mSendArrayList = (SendArrayList)getActivity();" gives class cast exception can anyone tell me how can i use the interface for send the data from one fragment to the another.

Upvotes: 0

Views: 82

Answers (2)

Rajan Kali
Rajan Kali

Reputation: 12953

Its simple,get instance of interface in onAttach

    private SendArrayList mCallback;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mCallback = (SendArrayList) context;
            dbh = new DatabaseHandler(getContext());
            Cursor cursor = dbh.getAllCustomer();
            if (cursor.moveToFirst()) {
              do {
                  String customerName = cursor.getString(0);
                  mListCustomerName.add(customerName);
                } while (cursor.moveToNext());
             }
            mCallback.sendData(mListCustomerName);
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement SendArrayList");
        }
  }

And in your Parent Activity, you need to implement SendArrayList interface

Upvotes: 0

Kona Suresh
Kona Suresh

Reputation: 1854

You get the Activity instance from the OnAttach() method.

public class HeadlinesFragment extends ListFragment {
        OnHeadlineSelectedListener mCallback;

        // Container Activity must implement this interface
        public interface OnHeadlineSelectedListener {
            public void onArticleSelected(int position);
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);

            // This makes sure that the container activity has implemented
            // the callback interface. If not, it throws an exception
            try {
                mCallback = (OnHeadlineSelectedListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString()
                        + " must implement OnHeadlineSelectedListener");
            }
        }

        ...
    }

The following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Send the event to the host activity
        mCallback.onArticleSelected(position);
    }

The Activity should implement the your Interface.then you send the data from fragment to activity, it will be shared to another fragment.

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Create your second fragment instance here and share your data (position)
    }
}

reference: https://developer.android.com/training/basics/fragments/communicating.html#Deliver

Upvotes: 1

Related Questions