Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Displaying Items in DialogFragment ListView Issue

Hello.

So the scenario here is that I have two fragments and my MainActivity. -The first fragment adds items to the second fragment (Which contains a ListView). -The second fragment displays the ListView. -The MainActivity is for passing the data of the item created in the first fragment over to the second.

I believe I have the passing data part down, but for some reason, the data is not displaying at all.

Here is the code for my MainActivity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//Declaring objects...

View.OnClickListener listenerList = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTodoListsFragment = new TodoListsDialogFragment();
                mTodoListsFragment.setListener(mListener4);
                mTodoListsFragment.show(getSupportFragmentManager(), TODO_LIST_FRAGMENT);
            }
        };
    }

// When adding a list
    @Override
    public void addListClickListener(View v, ArrayList<ListObject> objects, ListsAdapter adapter) {
        mNewListFragment = new NewListDialogFragment();
        mObjects = objects;
        mListsAdapter = adapter;
        mNewListFragment.setListener(mListener5);
        mNewListFragment.setListener1(mListener6);
        mTodoListsFragment.dismiss();
        mNewListFragment.show(getSupportFragmentManager(), NEW_LIST_FRAGMENT);
    }


// When the user creates a new list
    @Override
    public void createListListener(View v, String string, ListObject object) {
        mListObject = object;
        mListObject.setListName(string);
        mObjects.add(mListObject);
        mListsAdapter.notifyDataSetChanged();
    }

ListView Fragment:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Context context = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_todo_lists, null);
        builder.setView(view);

        mAddListImage = (ImageView) view.findViewById(R.id.addListImage);
        mCloseListImage = (ImageView) view.findViewById(R.id.closeListView);
        mNoListsView = (TextView) view.findViewById(R.id.noListsView);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.listsRecyclerView);
        mObjects = new ArrayList<>();

        RecyclerViewSetup();

        mAddListImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.addListClickListener(v, mObjects, mListsAdapter);
            }
        });

        mCloseListImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return dialog;
    }

Create list Dialog:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Context context = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_new_list, null);
        builder.setView(view);

        mConfirmAddListView = (TextView) view.findViewById(R.id.confirmAddListView);
        mDismissAddListView = (TextView) view.findViewById(R.id.dismissAddListView);
        mListEditText = (EditText) view.findViewById(R.id.listEditText);
        mListObject = new ListObject();

        mConfirmAddListView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item;
                item = mListEditText.getText().toString();
                if (item.equals("") || item.startsWith(" ")) {
                    String message = "Please fill in the empty field.";
                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                } else {
                    mListener1.createListListener(v, item, mListObject);
                    mListener.dismissDialogListener(v);
                    dismiss();
                }
            }
        });

        mDismissAddListView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.dismissDialogListener(v);
                dismiss();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return dialog;
    }

What am I doing wrong?

Thank you!

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

I'm just not seeing the data passed to the list view at any point. Your create list dialog is triggering the callback to MainActivity to create a list but it's never forwarded to the list view fragment. They both appear to have a list called mObjects but they are entirely separate lists in each class.

I'm declaring the mObjects array in MainActivity as the same one in the ListView fragment using an interface. Then in the createListListener, I'm calling mObjects.add(mListObject).

Seth Kroger
Seth Kroger
56,413 Points

You're allocating a new list every time you display the list fragment, wiping out the old one. Perhaps it's better to have the activity allocate a new list and keep any action to create/modify the list is kept in one place?

So I decided to try creating the mObjects array in the MainActivity, but somehow, I still need to define it in the ListFragment. I'm not sure what I'm suppose to do.