0

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

我添加了方法 validatePositionsList,它应该:从 OrderPage 中获取我的产品列表,然后再次编写我的名为 arrr 的字符串表并调用 notifyDataSetChanged 以刷新列表。最后一步不行。我检查了所有内容,唯一的问题是我的 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");
            }
    }

}
4

1 回答 1

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");
            }
于 2013-06-18T15:11:51.220 回答