列表视图中没有关于设备轮换的数据

时间:2016-10-26 13:25:40

标签: android loader android-cursorloader

一旦数据被加载并且所有现在设置如果我旋转设备列表视图已经消失并且没有更多数据我必须重新启动应用程序以加载数据,但是当设备离线并且我只是从表加载数据它甚至在轮换时保留数据

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Boolean>{

Toolbar tb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initilize();
    setSupportActionBar(tb);
    tb.setTitleTextColor(getResources().getColor(R.color.white));
    ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    if(isConnected){
        loadData();
    }else {
        Snackbar bar = Snackbar.make(findViewById(android.R.id.content), getResources().getString(R.string.noint), Snackbar.LENGTH_INDEFINITE).setAction("Dismiss", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        bar.show();
    }
    getSupportFragmentManager().beginTransaction().add(R.id.main_list,new lvfrag()).commit();
}

private void initilize() {
    tb = (Toolbar)findViewById(R.id.main_toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id)
    {
        case R.id.menu_refresh :
            break;
        case R.id.menu_setting :
            Intent is = new Intent(MainActivity.this,Prefs.class);
            startActivity(is);
            break;
        case R.id.menu_about:
            AlertDialog.Builder adb = new AlertDialog.Builder(this);
            adb.setTitle(getResources().getString(R.string.app_name));
            adb.setMessage(getResources().getString(R.string.abt));
            adb.create();
            adb.show();
            break;
    }
    return true;
}

@Override
public Loader<Boolean> onCreateLoader(int id, Bundle args) {
    return new Loade(this);
}

@Override
public void onLoadFinished(Loader<Boolean> loader, Boolean data) {
    setd(data);
}

private void setd(Boolean data) {
}


@Override
public void onLoaderReset(Loader<Boolean> loader) {
}

private void loadData() {
    if(getSupportLoaderManager().getLoader(0) == null) {
        getSupportLoaderManager().initLoader(0, null, this).forceLoad();
    } else {
        getSupportLoaderManager().restartLoader(0, null, this).forceLoad();
    }
}

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

    CustomA ca;
    ListView lv;
    private static final int mId = 1001;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.v("Added","");
        View v = inflater.inflate(R.layout.list_vw,container,false);
        ConnectivityManager cm = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        lv = (ListView)v.findViewById(R.id.frag_list);
        if(isConnected){
            getActivity().getContentResolver().delete(weathertable.mContentUri,null,null);
            ca = new CustomA(getActivity(),null);
            lv.setAdapter(ca);
            loadfData();
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent dt = new Intent(getActivity(),Detail.class);
                    dt.setData(Uri.withAppendedPath(weathertable.mContentUri,String.valueOf(id)));
                    getActivity().startActivity(dt);
                }
            });
        }else {
            Cursor c = getActivity().getContentResolver().query(weathertable.mContentUri,null,null,null,null);
            ca = new CustomA(getActivity(),c);
            lv.setAdapter(ca);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(getActivity(),getResources().getString(R.string.cnt),Toast.LENGTH_SHORT).show();
                }
            });
        }
        return v;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        switch (id){
            case mId:
                return new CursorLoader(getActivity(),weathertable.mContentUri,null,null,null,null);
        }
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        ca.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        ca.swapCursor(null);
    }

    private void loadfData() {
        if(getActivity().getSupportLoaderManager().getLoader(mId) == null) {
            getActivity().getSupportLoaderManager().initLoader(mId, null, this).forceLoad();
        } else {
            getActivity().getSupportLoaderManager().restartLoader(mId, null, this).forceLoad();
        }
    }

}

}

2 个答案:

答案 0 :(得分:0)

在android调用onConfigurationChanged中有一个方法需要覆盖以修复此问题。

基本上,当你旋转设备时,操作系统被迫完全重新实例化活动,并随之失去当前状态。

如果为适配器创建字段变量,然后在类中进一步设置变量,则可以在setAdapter中调用onConfigurationChanged将数据放回列表视图中

答案 1 :(得分:0)

添加:android:configChanges =“orientation | screenSize”到清单解决了问题