RecyclerView与sqlite db在活动中工作但不在片段中工作

时间:2018-05-04 12:27:24

标签: android android-fragments android-recyclerview

我是android的初学者 我找到了许多类似问题的答案,但遗憾的是没有答案对我有用 我正在使用recylcerView使用卡片视图来显示学生数据。我正在从我的sqlite数据库中获取数据。 我的代码在活动中运行良好,但由于导航抽屉我把它放在一个片段中,它显示为空白。 这是我的活动和片段代码。

MainActivity

public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private PersonDBHelper dbHelper;
private MyCustomAdapter adapter;
private String filter = "";
TextView textView;
public static int navItemIndex = 0;
Toolbar toolbar;
private DrawerLayout mDrawerLayout;
FloatingActionButton button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.person_list);
   /* toolbar=findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);*/
    mDrawerLayout=findViewById(R.id.drawer_layout);

    NavigationView navigationView=findViewById(R.id.nav_view);


    mRecyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    textView=findViewById(R.id.empty_view);


    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    //populate recyclerview
    populaterecyclerView(filter);




    button=findViewById(R.id.fab_add);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            goToAddUserActivity();
        }
    });
}

private void populaterecyclerView(String filter) {
    dbHelper = new PersonDBHelper(this);

    adapter = new MyCustomAdapter(dbHelper.peopleList(filter), this, mRecyclerView);
    mRecyclerView.setAdapter(adapter);

}

private void goToAddUserActivity(){
    Intent intent = new Intent(MainActivity.this, AddRecordActivity.class);
    startActivity(intent);
}

@Override
protected void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();
}

这工作正常,但对于片段我做了以下更改

StudentListFragment()

ublic class StudentListFragment extends Fragment {

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private PersonDBHelper dbHelper;
private MyCustomAdapter adapter;
FloatingActionButton button;
private String filter = "";
View rootView;
@RequiresApi(api = Build.VERSION_CODES.M)

public static StudentListFragment newInstance(){

    StudentListFragment studentListFragment = new StudentListFragment();
    Bundle args=new Bundle();
    studentListFragment.setArguments(args);
    return studentListFragment;
}

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


}




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




   rootView=inflater.inflate(R.layout.person_list,container,false);
     mRecyclerView=rootView.findViewById(R.id.recycler_view);
    dbHelper = new PersonDBHelper(getContext());
    adapter = new MyCustomAdapter(dbHelper.peopleList(filter), getContext(), mRecyclerView);

    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();


    button=rootView.findViewById(R.id.fab_add);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            goToAddUserActivity();
        }
    });
    return rootView;


}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(false);
    ensureList();

}




private void goToAddUserActivity() {
    Intent intent = new Intent(getActivity(), AddRecordActivity.class);
    startActivity(intent);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle("StudentList");
}
public  void ensureList(){
    if (dbHelper != null) {
        return;
    }
    View root = getView();
    if (root == null) {
        throw new IllegalStateException("Content view not yet created");

    }   }

}

我在导航抽屉代码中调用此片段正在运行但未显示任何数据。如果我把这个片段放在导航抽屉菜单中,这意味着当导航抽屉活动开始时,这个片段将会打开。在这种情况下,它显示我的卡片(我的数据),但如果我切换到其他片段并返回到此片段,则显示空白

1 个答案:

答案 0 :(得分:0)

在fragment,onCreateView()方法中,请给根视图充气并立即返回,不要做任何其他事情。

将您的逻辑实现到onViewCreated()。像这样的东西:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
   rootView=inflater.inflate(R.layout.person_list,container,false);
    return rootView;
    }

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    getActivity().setTitle("StudentList");

   mRecyclerView=view.findViewById(R.id.recycler_view);
    dbHelper = new PersonDBHelper(getContext());
    adapter = new MyCustomAdapter(dbHelper.peopleList(filter), getContext(), mRecyclerView);

    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();


    button=view.findViewById(R.id.fab_add);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            goToAddUserActivity();
        }
    });
}
你可以试试这个吗?并确保列表中有项目。