简体   繁体   中英

ArrayAdapter in my ListFragment won't refresh even when I use notifyDataSetChanged

I am working on Android project. In my OrderPage class which extends FragmentActivity I have List with my products. In my PositionsFragment class which extends ListFragment I implement List, which don't want to refresh.

I add method validatePositionsList which should: take List of my products from OrderPage, then write again my Table of Strings called arrr and call notifyDataSetChanged to refresh List. Last step won't work. I check everything and the only problem is that my ListFragment won't refresh. I am refreshing it wrong?

public class PositionsFragment extends ListFragment {

    static ArrayAdapter la;
    static List<String> positionsString;
    static String[] arrr;
    int fragNum;

    public static PositionsFragment init(int val) {
        positionsList = new PositionsFragment();

        // Supply val input as an argument.
        Bundle args = new Bundle();
        args.putInt("val", val);
        positionsList.setArguments(args);
        validatePositionsList();
        return positionsList;
    }

    /**
     * Retrieving this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    /**
     * The Fragment's UI is a simple text view showing its instance number and
     * an associated list.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        validatePositionsList();
        View layoutView = inflater.inflate(R.layout.fragment_order_list_positions,
                container, false);
        View tv = layoutView.findViewById(R.id.text);
        ((TextView) tv).setText("Pozycje zamówienia");
        return layoutView;
    }

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

        la = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, arrr);
        la.notifyDataSetChanged();
        setListAdapter(la);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("Truiton FragmentList", "Item clicked: " + id);
    }

    public static void validatePositionsList() {
        tempMyProducts = OrderPage.getProducts();
        positionsString = new ArrayList<String>();
        positionsString.add("mm");
        for(myProduct obj : tempMyProducts)
        {
            Log.e("validatePositionsList", String.valueOf(obj.getNazwa()));
            positionsString.add("mm");
        }
        arrr = Arrays.copyOf(positionsString.toArray(),positionsString.toArray().length,String[].class);
        if(la!=null) 
            {
            la.notifyDataSetChanged();
            Log.e("validatePositionsList", "Odswiezam");
            }
    }

}

I think you are changing array, but these changes not affects on adapter internal list. Try following code:

arrr = Arrays.copyOf(positionsString.toArray(),positionsString.toArray().length,String[].class);
        if(la!=null) 
            {
            la.clear();
            for (int i = 0; i < arrr.length; i++)
                la.add(arrr[i]);
            la.notifyDataSetChanged();
            Log.e("validatePositionsList", "Odswiezam");
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM