0

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;
}

}

anuradha
  • 692
  • 1
  • 9
  • 22

1 Answers1

0

Kindly check this answer if it helps you,

In your respective Fragment code create a public function

FragmentA.java create a function like below.

    public void getData(int r) {
        Log.e("data got from Activity", "" + r); // this is sample
        //Do your updations to list view here.
      }

Now in your mainActivity.java call this function like below wherever you want

button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        new FragmentA().getData(34);
      }
    });

Now check the log cat. you will see the number 34 is printed. Let me know if you have any queries.

Swaminathan V
  • 4,663
  • 2
  • 22
  • 32
  • please see my codes. May be my question is not clear to you. I want to access the ListView from a fragment class to an Activity class. Is that possible? If not, I will try something else. – anuradha Jul 18 '16 at 14:02
  • you want the listview inside the fragment to be updated when you click button in the activity ? correct ? – Swaminathan V Jul 18 '16 at 14:29