Trouble refreshing android array adapter in fragment

501 views Asked by At

I'm having trouble refreshing an array adapter. When I'm testing the program on a phone it works well and refreshes perfectly when switching through fragments. The problem comes when I'm using a tablet and both fragments are up at once.

Here's my fragment code:

public class HistoryFragment extends Fragment {

private ArrayList<String> history = new ArrayList<String>();
private ArrayAdapter<String> arrayAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle data = getArguments();
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.history, container, false);
    ListView view = (ListView) v.findViewById(R.id.list);

    if(data != null){
        history = data.getStringArrayList("history");
    }       
    arrayAdapter = new ArrayAdapter<String>(
            getActivity(), 
            android.R.layout.simple_list_item_1, history);

    view.setAdapter(arrayAdapter);

    return v;
}

public void setHistory(ArrayList<String> history) {
    history.addAll(history);
    arrayAdapter.notifyDataSetChanged();
}

}

Here's my activity code:

public class MainActivity extends Activity implements historyClickListener{

public static ArrayList<String> history = new ArrayList<String>();

CalcFragment mCalcFragment = new CalcFragment();
HistoryFragment mHistFragment = new HistoryFragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    if (findViewById(R.id.container) != null) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.add(R.id.container, mCalcFragment);
        fragmentTransaction.commit();
    }
}

@Override
public void sendHistory(ArrayList<String> history){
    Fragment hf = getFragmentManager().findFragmentById(R.id.historyfrag);

    if(hf != null){
        mHistFragment.setHistory(history);
    }else{
        Bundle data = new Bundle();
        data.putStringArrayList("history", history);
        mHistFragment.setArguments(data);
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container, mHistFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

The sendHistory is an interface method in a calculator that help the calculator send the list of equations that have been done. Then in main activity if I'm using a tablet then all it does is setHistory which is a method in the history fragment. I believe the problem is that I'm not using arrayAdapter.notifyDataSetChanged() in the history fragment correctly. Can someone please help. Thanks!

2

There are 2 answers

1
h0m3r16 On

You are setting the new data to a variable of the fragment not to the adapter who is the one who shows the data.

So you can:

  1. Create a new ArrayAdapter with the new data and set it to the listview.
  2. Extend ArrayAdapter, create a method that pass the new Data to the Adapter and then call notifyDataSetChanged().
1
km86 On

this.history.addAll(history)? I see the same variable name for parameter and member variable.