ListView不会删除第一个数据结果的空视图,但之后会删除

时间:2015-09-01 04:30:46

标签: android listview

根据这些帖子,我有一个正确显示我的空视图的ListView:https://stackoverflow.com/a/5565700/4527140

当ListView第一次填充数据时,它不会更新并显示数据。如果我做了一些事情来重新加载光标,它会显示数据。但即使试图强制重新加载第一批数据也不会删除空视图。

在我的下面的代码中,在第一次运行时,用户将选择一种语言,因此在onResume中,将调用onLanguageChanged(),然后调用游标上的restartLoader()。但它继续显示空视图。

MainActivity:

public class MainActivity {

    private static String mLanguage;
    private MyFragment mMyFragment;

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

        mLanguage = Utility.getActiveLanguageName(this);

        setContentView(R.layout.activity_main);

        myFragment =  ((MyFragment)getSupportFragmentManager()
                .findFragmentById(R.id.fragment_my));
        if (mMyFragment == null) {
            mMyFragment = new MyFragment();
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_my, mMyFragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        View empty = findViewById(R.id.empty);
        ListView list = (ListView) findViewById(R.id.listview_words);
        list.setEmptyView(empty);
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkSetup(); //check initialization setup
        String activeLanguage = Utility.getActiveLanguageName(this);
        if (!activeLanguage.equals(mLanguage)) { //if language has changed, reload listview
            mLanguage = activeLanguage;
            mMyFragment.onLanguageChange();
        }
    }
}

MyFragment:

public class MyFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private MyAdapter mMyAdapter;
    private static final int LIST_LOADER = 0;
    private ListView mListView;
    private int mIndex = -1;
    private int mTop = 0;


    public DictionaryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Context context = getActivity().getApplicationContext();
        mMyAdapter = new MyAdapter(getActivity(), null, 0);
        final View rootView = inflater.inflate(R.layout.fragment_my, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getLoaderManager().initLoader(LIST_LOADER, null, this);
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        mListView.setVisibility(View.GONE);
        mSpinner.setVisibility(View.VISIBLE);
        //switch between different queries based on user selections

        return new CursorLoader(getActivity(),
                query,
                null,
                null,
                null,
                null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mListView.setVisibility(View.VISIBLE);
        mSpinner.setVisibility(View.GONE);
        mMyAdapter.swapCursor(data);
        if(mIndex!=-1) {
            mListView.setSelectionFromTop(mIndex, mTop);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mMyAdapter.swapCursor(null);
        if(mIndex!=-1) {
            mListView.setSelectionFromTop(mIndex, mTop);
        }
    }

    public void reloadData() {
        getLoaderManager().restartLoader(LIST_LOADER, null, MyFragment.this);
    }

    public void onLanguageChange(){
        mIndex = 0;
        mTop = 0;
        reloadData();
    }
}

更新 稍后在语言更改时正确刷新列表视图。第一次运行时出现问题。

我在onLoadFinished()中尝试了以下所有内容,没有任何变化:

myAdapter.notifyDataSetChanged();
mListView.setAdapter(myAdapter);
mListView.invalidateViews();
mListView.refreshDrawableState();

刷新列表通常有一点延迟,其中列表设置为GONE并且微调器设置为VISIBLE使用第一次加载时,没有延迟,没有微调器,它只是直接到空视图。

随着更多的挖掘,我认为问题是基础数据。在初始设置过程中,正在从服务器检索数据并填充表。如果我在这些设置屏幕上等待几秒钟,在此期间数据将在后台加载,那么当我返回到ListView时,查询会产生结果并且它们会出现。因此,一旦加载了数据,CursorAdapter就会更新数据。

0 个答案:

没有答案