简体   繁体   English

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

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

I am working on Android project. 我正在从事Android项目。 In my OrderPage class which extends FragmentActivity I have List with my products. 在扩展FragmentActivity的OrderPage类中,我的产品具有列表。 In my PositionsFragment class which extends ListFragment I implement List, which don't want to refresh. 在扩展ListFragment的PositionsFragment类中,我实现了List,不想刷新。

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. 我添加了方法validatePositionsList,该方法应该:从OrderPage中获取我的产品列表,然后再次写入名为arrr的字符串表,并调用notifyDataSetChanged刷新列表。 Last step won't work. 最后一步不起作用。 I check everything and the only problem is that my ListFragment won't refresh. 我检查了所有内容,唯一的问题是我的ListFragment无法刷新。 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");
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用.notifyDataSetChanged()更新Listfragment中的SimpleCursorAdapter? - How to use .notifyDataSetChanged() to update SimpleCursorAdapter in Listfragment? 当从后端堆栈返回布局时,如何刷新ListFragment? - How can I refresh my ListFragment when it returns to the layout from back stack? 调用arrayAdapter.notifyDataSetChanged()时应用崩溃 - App crashed when arrayAdapter.notifyDataSetChanged() was called 当我使用notifyDataSetChanged()时,复选框刷新为false; - Checkbox refreshing in false when I use notifyDataSetChanged(); 即使我调用 notifyDataSetChanged(),RecyclerView 也不显示项目 - RecyclerView don't show items even if I call notifyDataSetChanged() notifyDataSetChanged()在ListFragment中不起作用 - notifyDataSetChanged() not working in ListFragment 在ListFragment中显示自定义ArrayAdapter - Show Custom ArrayAdapter in a ListFragment 如何在另一个线程上的回调内的ArrayAdapter中notifyDataSetChanged? -安卓 - How do I notifyDataSetChanged in ArrayAdapter inside a callback on a different thread? - Android ArrayAdapter 未使用 notifyDataSetChanged() 进行更新 - ArrayAdapter not updating with notifyDataSetChanged() 我的Eclipse火星不会刷新 - My Eclipse Mars won't refresh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM