Issue
I have MainActivity that holds three tabs. In each tab I'm displaying data taken from SQLite db as list using RecyclerView.
When I first launch app the data is shown properly. After I return to previously opened tab the data is not there.
The problem started to appear after I have implemented following:
- Check if adapter is null.
- If yes, create adapter instance and fill it with data.
- If no, just update the data inside of adapter and call
notifyDataSetChanged()
This idea was working in other project where I had to do same with Activity. In this case I have fragment. I have just get to know that Fragment has different lifecycle behavior.
How I can solve this problem?
One of the tabs class:
public class AllTabView extends Fragment {
Context ourContext;
RecyclerView mList;
AllAdapter adapter;
RecyclerView.LayoutManager layoutManager;
TextView mEmptyText;
long movedItem, draggedItem;
ArrayList<ArrayList<String>> mDays;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v1 = inflater.inflate(R.layout.list_view_content, container, false);
mList = (RecyclerView) v1.findViewById(R.id.recycler_view);
mEmptyText = (TextView) v1.findViewById(R.id.empty_text);
ourContext = getContext();
layoutManager = new LinearLayoutManager(ourContext);
mList.setLayoutManager(layoutManager);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
mList.setItemAnimator(itemAnimator);
return v1;
}
@Override
public void onResume() {
super.onResume();
getDays();
if (mDays.size() == 0) {
mList.setVisibility(View.GONE);
mEmptyText.setVisibility(View.VISIBLE);
} else {
mList.setVisibility(View.VISIBLE);
mEmptyText.setVisibility(View.GONE);
if(adapter == null) {
Log.v(TAG, "adapter null");
adapter = new AllAdapter(ourContext, mDays);
}
else {
Log.v(TAG, "notify");
adapter.updateData(mDays);
}
mList.setAdapter(adapter);
}
}
public void getDays() {
DaysDatabase info = new DaysDatabase(ourContext);
info.open();
mDays = info.getData();
info.close();
}
public void setAdapter() {
adapter = new AllAdapter(ourContext, mDays);
mList.setAdapter(adapter);
}
}
MainActivity that holds fragments:
public class MainActivity extends AppCompatActivity {
FragmentTabHost mFragmentTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mFragmentTabHost.setup(MainActivity.this, getSupportFragmentManager(), android.R.id.tabcontent);
String tag1 = "all";
String tag2 = "since";
String tag3 = "until";
final View tabView1 = createTabView(MainActivity.this, "All");
final View tabView2 = createTabView(MainActivity.this, "Since");
final View tabView3 = createTabView(MainActivity.this, "Until");
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec(tag1).setIndicator(tabView1).setContent(new FragmentTabHost.TabContentFactory() {
public View createTabContent(String tag1) {
return tabView1;
}
}), AllTabView.class, null);
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec(tag2).setIndicator(tabView2).setContent(new FragmentTabHost.TabContentFactory() {
public View createTabContent(String tag2) {
return tabView2;
}
}), SinceTabView.class, null);
mFragmentTabHost.addTab(mFragmentTabHost.newTabSpec(tag3).setIndicator(tabView3).setContent(new FragmentTabHost.TabContentFactory() {
public View createTabContent(String tag1) {
return tabView3;
}
}), UntilTabView.class, null);
mFragmentTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
Solution
I have edited your code. Please take a look.
public class AllTabView extends Fragment {
Context ourContext;
RecyclerView mList;
AllAdapter adapter;
RecyclerView.LayoutManager layoutManager;
TextView mEmptyText;
long movedItem, draggedItem;
ArrayList<ArrayList<String>> mDays = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v1 = inflater.inflate(R.layout.list_view_content, container, false);
mList = (RecyclerView) v1.findViewById(R.id.recycler_view);
mEmptyText = (TextView) v1.findViewById(R.id.empty_text);
ourContext = getContext();
layoutManager = new LinearLayoutManager(ourContext);
mList.setLayoutManager(layoutManager);
getDays();
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
mList.setItemAnimator(itemAnimator);
adapter = new AllAdapter(ourContext, mDays);
mList.setAdapter(adapter);
return v1;
}
@Override
public void onResume() {
super.onResume();
getDays();
if (mDays.size() == 0) {
mList.setVisibility(View.GONE);
mEmptyText.setVisibility(View.VISIBLE);
} else {
mList.setVisibility(View.VISIBLE);
mEmptyText.setVisibility(View.GONE);
}
}
public void getDays() {
DaysDatabase info = new DaysDatabase(ourContext);
info.open();
mDays.clear();
mDays.addAll(info.getData());
if (adapter != null)
adapter.notifyDataSetChanged();
info.close();
}
}
Answered By - Aman Jain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.