1

I have two fragments that I want to be able to communicate together. In my FragmentPlannerAddItem I want to be able to create an event or task (in the code I actually call these "activities" or "activityItem") like in Google Calenadar. The information about the event (time, date, name and so on) is entered in the FragmentPlannerAddItem fragment and then sent to the FragmentPlanner fragment, which is the fragment where the user can get an overview of the planned events (I have a ListView in FragmentPlanner).

The problem is that when I create an item in FragmentPlannerAddItem, the item is then not displayed on FragmentPlanner. This is strange, because I am fully able to create two items in the list (they are test items) on Fragment Planner, but when I later add an item from FragmentPlannerAddItem the new item is not visible.

I have made the communication between the fragments by letting both FragmentPlanner and FragmentPlannerAddItem define their own interfaces with a method. These interfaces are then implemented by HostMainActivity, to which these two fragments are bound. So when FragmentPlannerAddItem sends information to FragmentPlanner it does so by first sending it to HostMainActivity and the HostMainActivity then sends it to FragmentPlanner.

I have runned debugging on this, and found out, that the list that contain my row items DOES contain this new item. However, it is not displayed along with those test items that are created when upon startup. I have gotten a hint from my professor that it may have something to do with my Custom Adapter (because the rest of my code seems to look fine, he says). He did not have time to go through the entire adapter. Something happens in my Custom Adapter that seems to be messing something up. I have tried using different adapters, but I struggle to understand how to set it up properly. I have no idea, what code is essential for adapters to work and what code is non-essential and may do strange things.

In summary, I want the user to be able to create an event/task on the FragmentPlannerAddItem fragment, and then have the events displayed on a list with their appropriate times and checked/unchecked boxes. Right now I just want to get some row_items to appear on FragmentPlanner when created on FragmentPlannerAddItem. Assuming the problem is somewhere in my custom adapter, can anyone help identifying what might be wrong in this adapter?

Thanks in advance. Please ask if I am being too unclear with anything I have described.

Kind Regards Andreas

In CustomAdapter

public class CustomAdapter extends ArrayAdapter<ActivityItem> implements View.OnClickListener{

private ArrayList<ActivityItem> dataSet;
Context mContext;

// View lookup cache
private static class ViewHolder {
    TextView actName;
    TextView actTime;
    CheckBox actCheckbox;
}

public CustomAdapter(ArrayList<ActivityItem> data, Context context) {
    super(context, R.layout.row_item, data);
    this.dataSet = data;
    this.mContext=context;

}

public void addItem(String name, String time, String date){
    dataSet.add(new ActivityItem(name, new Date(0)));
    notifyDataSetChanged();
}

private int lastPosition = -1;

@Override
public int getCount() {
    return dataSet.size();
}

@Override
public ActivityItem getItem(int i) {
    return dataSet.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

// @SuppressLint("WrongViewCast")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Get the data item for this position
    ActivityItem activityItem = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag

    final View result;

    if (convertView == null) {

        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(R.layout.row_item, parent, false);
        viewHolder.actName = (TextView) convertView.findViewById(R.id.description);
        viewHolder.actTime = (TextView) convertView.findViewById(R.id.timeField);
        viewHolder.actCheckbox = (CheckBox) convertView.findViewById(R.id.checkBox);

        result=convertView;

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }

    /*Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
    result.startAnimation(animation);
    lastPosition = position;*/

    viewHolder.actName.setText(activityItem.getName());
    viewHolder.actTime.setText(activityItem.getTime().toString());
    viewHolder.actCheckbox.setChecked(activityItem.getChecked());
   /* viewHolder.txtType.setText(activityItem.getType());
    viewHolder.txtVersion.setText(activityItem.getVersion_number());
    viewHolder.info.setOnClickListener(this);
    viewHolder.info.setTag(position);*/
    // Return the completed view to render on screen
    return convertView;
}

}

Umair Iqbal
  • 1,959
  • 1
  • 19
  • 38
  • You shouldn't have a mutable ArrayList inside your adapter and you shouldn't modify it in place, the adapter should only how to map a list of items to a list of Views that you set up according to the items' state. A method such as `updateItems(List)` that gets `Collections.unmodifiableList(list)` would prevent this kind of mistake. – EpicPandaForce Nov 30 '19 at 19:22
  • My problem is now solved! I have gotten help from one of the teaching assistants at my university. Appearantly, my Custom adapter was fine. The TA provided a solution by using bundles. By following this post, one can solve a similiar problem as mine: https://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment – Andreas Madsen Dec 02 '19 at 12:22

0 Answers0