I have implemented a TabLayout
in android with two fragments. In one fragment I added a ListView
. I am retrieving data from server to fill up the ListView
. Everything was working fine until I tried to access the ListView
from the MainActivity
class.
I need access of the same listview(which is in the fragment) from the MainActivity where I created the TabLayout
to load data. The fragment is created for a tab of the tablayout.
I added the code below.
Can anyone tell me how can I access that ListView
from the MainActivity
?
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_report);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_launcher));
tabLayout.addTab(tabLayout.newTab().setIcon(R.mipmap.ic_launcher));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setVisibility(View.INVISIBLE);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
ReportListFragment.java
public class ReportListFragment extends Fragment{
private SimpleDateFormat mFormatter = new SimpleDateFormat("MMMM dd yyyy hh:mm aa");
Date newDate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
finder_sharedprefs = getActivity().getSharedPreferences(FINDERPREFERENCES, Context.MODE_PRIVATE);
listView=(ListView) view.findViewById(R.id.list);
reportLayout = (RelativeLayout) view.findViewById(R.id.new_report);
} catch (ParseException e) {
e.printStackTrace();
}
}
return view;
}
}