即使我使用notifyDataSetChanged,我的ListFragment中的ArrayAdapter也不会刷新

时间:2013-06-18 14:56:51

标签: java android android-arrayadapter

我正在开发Android项目。在扩展FragmentActivity的OrderPage类中,我使用了我的产品列表。在我的PositionsFragment类中,它扩展了ListFragment,我实现了List,它不想刷新。

我添加方法validatePositionsList应该:从OrderPage获取我的产品列表,然后再次写入名为arrr的字符串表并调用notifyDataSetChanged来刷新List。最后一步不起作用。我检查一切,唯一的问题是我的ListFragment不会刷新。我刷新它错了吗?

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");
            }
    }

}

1 个答案:

答案 0 :(得分:0)

我认为您正在更改阵列,但这些更改不会影响适配器内部列表。 请尝试以下代码:

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");
            }