How to make a ListView from the Bundle? - android

My login activity needs LoginSuccess where my fragments are loaded there.
LoginActivity.java:
Intent intent = new Intent(LoginActivity.this,LoginSuccess.class);
Bundle bundle = new Bundle()
bundle.putString("first_name",jsonObject.getString("first_name"));
bundle.putString("last_name",jsonObject.getString("last_name"));
bundle.putString("username",jsonObject.getString("username"));
bundle.putString("email",jsonObject.getString("email"));
bundle.putString("mobile",jsonObject.getString("mobile"));
bundle.putString("appointments",jsonObject.getString("appointments"));
intent.putExtras(bundle);
startActivity(intent);
How can I fetch the bundle "appointments" in a fragment? Thanks in advance.
AppointmentsFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
super.onCreate(savedInstanceState);
Bundle bundle = getActivity().getIntent().getExtras();
View v = inflater.inflate(R.layout.fragment_appointments, container, false);
String [] datasource = {"appointments", bundle.getString("appointments")};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.rowlayout, R.id.txtitem, datasource);
setListAdapter(adapter);
setRetainInstance(true);
Log.d("appointments", bundle.getString("appointments"));
View appointment_date = v.findViewById(R.id.appointment_date);
((TextView)appointment_date).setText(bundle.getString("appointment_date"));
return v;
}
Here's the code for LoginSuccess.java
`public class LoginSuccess extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_success);
toolbar = (Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new ProfileFragment(), "Profile");
viewPagerAdapter.addFragments(new AppointmentsFragment(), "Appointments");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}`

First open the Fragment by doing this inside your LoginActivity
LoginActivity.class
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import com.example.vostro.myapplication.fragments.FragmentDemo;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_main);
Bundle bundle = new Bundle();
bundle.putString("first_name",jsonObject.getString("first_name"));
bundle.putString("last_name",jsonObject.getString("last_name"));
bundle.putString("username",jsonObject.getString("username"));
bundle.putString("email",jsonObject.getString("email"));
bundle.putString("mobile",jsonObject.getString("mobile"));
bundle.putString("appointments",jsonObject.getString("appointments"));
FragmentDemo fragment = new FragmentDemo();
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.navigation_container, fragment);
fragmentTransaction.commit();
}
}
Then inside your new Fragment get the data by doing this
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.vostro.myapplication.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class FragmentDemo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_demo, null);
String jsonArrayAppointments = getArguments().getString("appointments");
try {
JSONArray jsonArray = new JSONArray(jsonArrayAppointments);
for (int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String yourRequiredField = jsonObject.getString("yourRequiredField");
}
} catch (JSONException e) {
e.printStackTrace();
}
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
login_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2196F3"
android:minHeight="?attr/actionBarSize"></android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textSize="20sp"
android:gravity="center"
android:text="#string/text"/>
<!-- This will hold your Fragment -->
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</LinearLayout>

If you want to sent data from activity to fragment then you have to use this code:
Bundle bundle = new Bundle()
bundle.putString("first_name",jsonObject.getString("first_name"));
bundle.putString("last_name",jsonObject.getString("last_name"));
bundle.putString("username",jsonObject.getString("username"));
bundle.putString("email",jsonObject.getString("email"));
bundle.putString("mobile",jsonObject.getString("mobile"));
bundle.putString("appointments",jsonObject.getString("appointments"));
YourFragmentClass yfc = new YourFragmentClass();
yfc.setArguments(bundle);
Fetch value from onCreateView() method in your "YourFragmentClass" like this:
String strappointments = getArguments().getString("appointments");

While adding the fragment follow this
I hope you are adding the fragment in LoginSuccess Activity.So in that activity get the arguments like
Bundle args = getIntent().getExtras();
While instantiating a fragment, follow this
Fragment mainFragment = new MyFragment();
mainFragment.setArguments(args)
getFragmentManager().beginTransaction().add(R.id.fragment_holder,mainFragment,"my_fragment").commit();
In your fragment get your arguments using getArguments() method
In your fragment use the following snippet to get the object of json.
Bundle args = getArguments();
String appointments = args.getString("appointments");
JSONParser parser = new JSONParser();
try{
Object obj = parser.parse(appointments);
JSONArray array = (JSONArray)obj;
for(Object appointment : array)
{
appObject = (JSONObject) appointment;
/* for getting data here like appointment status use,*/
String status = appObject.get("appointment_status");
}

Related

Android TabLayout not working in Fragment

I use the code below to setup my TabLayout inside a Fragment. The problem is the tabs don't show at all and neither does the child Fragment. Instead I get a blank page. The same code works in the parent Activity, with a minor difference in line PostsFragment.ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager()); which instead is ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());. What am I doing wrong?
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.tabs.TabLayout;
import com.google.firebase.quickstart.auth.R;
import com.google.firebase.quickstart.auth.social.Main_Chat_Activity;
import com.google.firebase.quickstart.auth.social.bottomnav.ui.dashboard.DashboardFragment;
import com.google.firebase.quickstart.auth.social.friends.ContactsFragment;
import com.google.firebase.quickstart.auth.social.messages.ChatFragment;
import com.google.firebase.quickstart.auth.social.profile.Profile_Fragment;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class PostsFragment extends Fragment {
private ViewPager mViewPager;
private TabLayout mTabLayout;
private View curView;
public PostsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
curView = inflater.inflate(R.layout.fragment_posts, container, false);
mViewPager = curView.findViewById(R.id.postTabsViewPager);
setupViewPager(mViewPager);
mTabLayout = curView.findViewById(R.id.postsTabs);
mTabLayout.setupWithViewPager(mViewPager);
setupTabTexts();
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_posts, container, false);
}
private void setupViewPager(ViewPager viewPager) {
PostsFragment.ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFrag(new FoundFragment(), "");
adapter.addFrag(new FoundFragment(), "");
adapter.addFrag(new FoundFragment(), "");
viewPager.setAdapter(adapter);
}
private void setupTabTexts() {
mTabLayout.getTabAt(0).setText("Tab 1");
mTabLayout.getTabAt(1).setText("Tab 2");
mTabLayout.getTabAt(2).setText("Tab 3");
}
static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#NotNull
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
The fragment_posts.xml is;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
tools:context=".social.posts.PostsFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/postsTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabSelectedTextColor="#color/black"
app:tabInlineLabel="true"
>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/postTabsViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/postsTabs"
>
</androidx.viewpager.widget.ViewPager>
</RelativeLayout>
When you are in a Fragment you should initialize and setup your ViewPager in onViewCreated() method instead in onCreateView().
Make the below changes:
1.In onCreateView() inflate only your layout like below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_posts, container, false);
}
2.In onViewCreated() initialize and setup your Pager with TabLayout like below:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewPager = view.findViewById(R.id.postTabsViewPager);
mTabLayout = view.findViewById(R.id.postsTabs);
mTabLayout.setupWithViewPager(mViewPager);
setupViewPager(mViewPager);
setupTabTexts();
}

Setting viewpager with same fragment is not working as expected

I am trying to set up a view pager containing three tab with same fragment.
But its not working as expected. I wanted to show individual tab with different background color and textview showing its ID based on the ID that I am providing using bundle and argument.
I am providing the code and pictures to understand better.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bdtask.myapplication.MainActivity">
<LinearLayout
android:id="#+id/tabviewholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#android:color/white"
/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/viewpagertab" />
</LinearLayout>
BlankFragment.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/container"
android:layout_height="match_parent"
tools:context="com.bdtask.myapplication.BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:gravity="center"
android:textSize="40sp"
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
MainActivity.java
package com.bdtask.myapplication;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TableLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter viewPagerAdapter = new
ViewPagerAdapter(getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.viewpager);
TabLayout tabLayout = findViewById(R.id.viewpagertab);
for(int i = 0 ; i<3 ; i++){
Bundle bundle = new Bundle();
bundle.putString("id",String .valueOf(i));
BlankFragment blankFragment = new BlankFragment();
blankFragment.setArguments(bundle);
viewPagerAdapter.getFragments(blankFragment, "tab "+i);
}
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
BlankFragment.java
package com.bdtask.myapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
TextView textView;
FrameLayout frameLayout;
String id;
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//
id = getArguments().getString("id");
return inflater.inflate(R.layout.fragment_blank, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
frameLayout = getActivity().findViewById(R.id.container);
textView = getActivity().findViewById(R.id.textview);
setbackgroundandtext();
}
public void setbackgroundandtext() {
if(id.equals("0")){
frameLayout.setBackgroundColor(Color.parseColor("#ab44ab"));
textView.setText(id);
}
if(id.equals("1")){
frameLayout.setBackgroundColor(Color.parseColor("#ea32ea"));
textView.setText(id);
}
if(id.equals("2")){
frameLayout.setBackgroundColor(Color.parseColor("#67ae34"));
textView.setText(id);
}
}
}
ViewPagerAdapter.java
package com.bdtask.myapplication;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.ArrayList;
/**
* Created by Jibunnisa on 5/20/2017.
*/
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<Fragment> fragments = new ArrayList<Fragment>();
ArrayList<String> titles = new ArrayList<String>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public void getFragments(Fragment fragment, String title) {
this.fragments.add(fragment);
this.titles.add(title);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
output:
You're setting id here to be 0,1,2:
for(int i = 0 ; i<3 ; i++) {
Bundle bundle = new Bundle();
bundle.putString("id",String .valueOf(i));
BlankFragment blankFragment = new BlankFragment();
blankFragment.setArguments(bundle);
viewPagerAdapter.getFragments(blankFragment, "tab "+i);
}
But here you're checking if it is 1,2,3:
public void setbackgroundandtext() {
if(id.equals("1")){
frameLayout.setBackgroundColor(Color.parseColor("#ab44ab"));
textView.setText(id);
}
if(id.equals("2")){
frameLayout.setBackgroundColor(Color.parseColor("#ea32ea"));
textView.setText(id);
}
if(id.equals("3")){
frameLayout.setBackgroundColor(Color.parseColor("#67ae34"));
textView.setText(id);
}
}
So check if if it is 0,1,2 instead.
EDIT:
You need to find the views inside the fragment, not through the activity, move the findViewById code from onActivityCreated to onCreateView.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//
id = getArguments().getString("id");
View v =inflater.inflate(R.layout.fragment_blank, container, false);
frameLayout = v.findViewById(R.id.container);
textView = v.findViewById(R.id.textview);
return v;
}

Can't set adapter of ListView because the ListView has a null object reference

I am trying to make a TabLayout with a ViewPager, but whenever I leave the app (Don't close it, just switch to another app and back) the fragments kind of get killed. I don't know that for sure, but the fragment's need to reCreate, and then fail.
The code for creating the TabLayout, TabLayoutAdapter and the ViewPager:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
MyTabLayoutAdapter tabLayoutAdapter;
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout.addTab(tabLayout.newTab().setText("-2"));
tabLayout.addTab(tabLayout.newTab().setText("-1"));
tabLayout.addTab(tabLayout.newTab().setText("0"));
tabLayout.addTab(tabLayout.newTab().setText("1"));
tabLayout.addTab(tabLayout.newTab().setText("2"));
tabLayoutAdapter = new MyTabLayoutAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(tabLayoutAdapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(2);
The code of my TabLayoutAdapter:
public class MyTabLayoutAdapter extends FragmentStatePagerAdapter {
private MyFragment fragmentOne;
private MyFragment fragmentTwo;
private MyFragment fragmentDefault;
private MyFragment fragmentFour;
private MyFragment fragmentFive;
private int numberOfTabs;
public MyTabLayoutAdapter(FragmentManager fragmentManager, int numberOfTabs) {
super(fragmentManager);
fragmentOne = new MyFragment();
fragmentTwo = new MyFragment();
fragmentDefault = new MyFragment();
fragmentFour = new MyFragment();
fragmentFive = new MyFragment();
this.numberOfTabs = numberOfTabs;
}
#Override
public MyFragment getItem(int position) {
switch (position) {
case 0:
return fragmentOne;
case 1:
return fragmentTwo;
case 2:
return fragmentDefault;
case 3:
return fragmentFour;
case 4:
return fragmentFive;
default:
return fragmentDefault;
}
}
#Override
public int getCount() {
return numberOfTabs;
}
}
The MyFragment fragment:
public class MyFragment extends Fragment {
private View currentView;
private ListView listView;
public MyFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
currentView = LayoutInflater.from(getContext()).inflate(R.layout.my_fragment_layout, container, false);
listView = (ListView) currentView.findViewById(R.id.listview);
return currentView;
}
//This line throws a NullPointer when I resume the app
public void populateListView(MyListAdapter myListAdapter) {
listView.setAdapter(myListAdapter);
}
}
Whenever I call
MyListAdapter myListAdapter = new MyListAdapter(this, R.layout.my_item, justSomeStringArray);
tabLayoutAdapter.getItem(0).populateListView(myListAdapter);
I get a NullPointerException saying that it can't set a adapter on a null object reference.
Anyone any idea how I can fix this?
Here's an approach which lets the Fragment instance drive the data retrieval by using an AsyncTask implementation (GetDataForListViewTask).
But, to follow Commonsware's advice, GetDataForListViewTask merely obtains the data. It does not set up the adapter for use on the ListViews. Instead, setting up the ListAdapter is done in the Fragment itself.
GetDataForListViewTask passes the data back to the Fragment instance using EventBus. This keeps GetDataForListViewTask from having to maintain a reference to the ListView, Activity, Context or any of that risky stuff.
And, by not "reaching in" to a Fragment from the outside to change its widgets or even hand it data, this code also tries (very broadly) to follow another piece of CommonsWare's guidance:
In general, having code outside of a fragment attempt to manipulate
the widgets inside that fragment — as you are doing here — is a bad
idea.
In any case, if you use this approach, which is a bit different from your original approach, you'll want to modify the GetDataForListViewTask so that it obtains the data from elsewhere in your app or the network, etc.
GetDataForListViewTask.java
import android.os.AsyncTask;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
class GetDataForListViewTask extends AsyncTask<Void, Void, ArrayList<String>> {
#Override
protected ArrayList<String> doInBackground(Void... ignored) {
// since this method runs on a worker thread, you may
// replace this code with a network call or a background computation
// from another part of the app is ready
ArrayList<String> listItems = new ArrayList<>();
for (int i=0; i<10; i++) {
listItems.add("item " + i);
}
return listItems;
}
#Override
protected void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
// post event so that Fragment can use the data to populate its ListView
EventBus.getDefault().post(new MyFragment.ListViewDataReadyEvent(result));
}
}
MainActivity.java
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
MyAdapter tabLayoutAdapter = new MyAdapter(getSupportFragmentManager(), 5);
viewPager.setAdapter(tabLayoutAdapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(2);
}
class MyAdapter extends FragmentStatePagerAdapter {
private final int count;
public MyAdapter(FragmentManager fm, int count) {
super(fm);
this.count = count;
}
#Override
public MyFragment getItem(int position) {
return MyFragment.newInstance();
}
#Override
public int getCount() {
return this.count;
}
#Override
public CharSequence getPageTitle(int position) {
return "Tab " + position;
}
}
}
MyFragment.java
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
public class MyFragment extends Fragment {
private ListView listView;
private GetDataForListViewTask getDataForListViewTask;
public static MyFragment newInstance() {
return new MyFragment();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true); // to support use of GetDataForListViewTask
EventBus.getDefault().register(this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
listView = (ListView) LayoutInflater.from(getContext()).inflate(
R.layout.my_fragment_layout, container, false);
getDataForListViewTask = new GetDataForListViewTask();
getDataForListViewTask.execute();
return listView;
}
#Override
public void onDestroy() {
if (getDataForListViewTask != null) {
getDataForListViewTask.cancel(false);
}
EventBus.getDefault().unregister(this);
super.onDestroy();
}
static class ListViewDataReadyEvent {
ArrayList<String> data;
ListViewDataReadyEvent(ArrayList<String> data) {
this.data = data;
}
}
#SuppressWarnings("unused")
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(ListViewDataReadyEvent event) {
if (listView != null) {
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(listView.getContext(),
android.R.layout.simple_list_item_1, event.data.toArray(new String[]{}));
listView.setAdapter(listAdapter);
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill" />
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
my_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#+id/listview"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</ListView>
Selected compile statements for build.gradle
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:appcompat-v7:25.3.1'
onCreateView() is not called on a fragment until the fragment is being added to a FragmentManager. Your final code snippet will only work, at best, if it is called sometime after fragmentOne is added to a FragmentManager. Otherwise, the fragment's ListView will not exist yet.
In general, having code outside of a fragment attempt to manipulate the widgets inside that fragment — as you are doing here — is a bad idea.

ViewPager showing fragments with the same data when should be treating each fragment individually

I am a relative newbie to android programming and I am having some issues. I'm am trying to attach fragments to a view pager for a todo application. I have 7 fragments that repersent each day. Each fragment loads correctly, but when I add a task, it shows across all fragments instead of only the one I entered the task in. Code is below. Any help would be great, thank you!
ViewPager Activity
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseUser;
public class ToDoActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
Parse.initialize(this, "uD7", "BrZkpvhY6Asi5oSq");
//ParseAnalytics.trackAppOpened(getIntent());
ParseObject.registerSubclass(Task.class);
ParseUser currentUser = ParseUser.getCurrentUser();
if(currentUser == null){
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(ToDoListFragment.newInstance("Important And Urgent"));
fList.add(ToDoListFragment.newInstance("Important Not Urgent"));
fList.add(ToDoListFragment.newInstance("Not Important But Urgent"));
return fList;
}
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
}
FragmentActivity.java
public class ToDoListFragment extends Fragment implements AdapterView.OnItemClickListener {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
private EditText mTaskInput;
private ListView mListView;
private TaskAdapter mAdapter = null;
private Button mButton;
public static final ToDoListFragment newInstance(String message)
{
ToDoListFragment f = new ToDoListFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.important_urgent, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText(message);
mAdapter = new TaskAdapter(getActivity(), new ArrayList<Task>());
mTaskInput = (EditText) v.findViewById(R.id.task_input);
mListView = (ListView) v.findViewById(R.id.task_list);
mButton = (Button) v.findViewById(R.id.submit_button);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mTaskInput.getText().length() > 0){
Task t = new Task();
t.setACL(new ParseACL(ParseUser.getCurrentUser()));
t.setUser(ParseUser.getCurrentUser());
t.setDescription(mTaskInput.getText().toString());
t.setCompleted(false);
t.saveEventually();
mAdapter.insert(t, 0);
mTaskInput.setText("");
}
}
});
updateData();
return v;
}
public void updateData(){
ParseQuery<Task> query = ParseQuery.getQuery(Task.class);
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
query.findInBackground(new FindCallback<Task>() {
#Override
public void done(List<Task> tasks, ParseException error) {
if(tasks != null){
mAdapter.clear();
for (int i = 0; i < tasks.size(); i++) {
mAdapter.add(tasks.get(i));
}
}
}
});
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Task task = mAdapter.getItem(position);
TextView taskDescription = (TextView) view.findViewById(R.id.task_description);
task.setCompleted(!task.isCompleted());
if(task.isCompleted()){
taskDescription.setPaintFlags(taskDescription.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}else{
taskDescription.setPaintFlags(taskDescription.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
task.saveEventually();
}
}
FragmentXML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Hello"
android:layout_gravity="center"
android:id="#+id/textView"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#color/red"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/task_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:hint="Enter a Task">
<requestFocus />
</EditText>
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
<ListView
android:id="#+id/task_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
If you are using the same fragment XML file for each fragment, then when you update one, you are going to update all of them. You would need to create 7 different XML layouts (they could be the same layout, but would need different identifiers), and update that fragment layout individually.
Fragments are basically individual activities, running in a single Activity. You can reuse Fragments in different Activities, but each Fragment should have it's own layout.

Dynamically changing the fragments inside a fragment tab host?

I have one main activity which is fragment activity here I am setting two tabs with two fragments A and B in the B fragment I have one button when the user click on the button I want to change fragment B to fragment C. But the tabs above are visible...
How I can achieve replacing fragments inside tabs?
Any solution are greatly appreciated.
Basic concept- We can achieve this by creating a container. Each tab will be assigned with a specific container. Now when we need a new fragment then we will replace same using this container.
Kindly follow undermentioned code step by step to have better understanding.
Step-1: Create Tabs for your app. Say "Home.java". It will contain code for creating tabs using fragment.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.TextView;
import app.drugs.talksooner.container.GoContainerFragment;
import app.drugs.talksooner.container.LearnContainerFragment;
import app.drugs.talksooner.container.MoreContainerFragment;
import app.drugs.talksooner.container.TalkContainerFragment;
import app.drugs.talksooner.container.WatchContainerFragment;
import app.drugs.talksooner.utils.BaseContainerFragment;
public class Home extends FragmentActivity {
private static final String TAB_1_TAG = "tab_1";
private static final String TAB_2_TAG = "tab_2";
private static final String TAB_3_TAG = "tab_3";
private static final String TAB_4_TAG = "tab_4";
private static final String TAB_5_TAG = "tab_5";
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
initView();
}
private void initView() {
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
// mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator("Talk", getResources().getDrawable(R.drawable.ic_launcher)), TalkContainerFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator("Talk"), TalkContainerFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_2_TAG).setIndicator("Learn"), LearnContainerFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_3_TAG).setIndicator("Go"), GoContainerFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_4_TAG).setIndicator("Watch"), WatchContainerFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec(TAB_5_TAG).setIndicator("More"), MoreContainerFragment.class, null);
/* Increase tab height programatically
* tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 150;
*/
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
final TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
if (tv == null)
continue;
else
tv.setTextSize(10);
}
}
#Override
public void onBackPressed() {
boolean isPopFragment = false;
String currentTabTag = mTabHost.getCurrentTabTag();
if (currentTabTag.equals(TAB_1_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_1_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_2_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_2_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_3_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_3_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_4_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_4_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_5_TAG)) {
isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(TAB_5_TAG)).popFragment();
}
if (!isPopFragment) {
finish();
}
}
}
Your home.xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Step-2: Define Base container fragment which will be helpful for backtracking and replacment of fragments "check out replaceFragement() ". Our class "BaseContainerFragment.java"
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import app.drugs.talksooner.R;
public class BaseContainerFragment extends Fragment {
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.replace(R.id.container_framelayout, fragment);
transaction.commit();
getChildFragmentManager().executePendingTransactions();
}
public boolean popFragment() {
Log.e("test", "pop fragment: " + getChildFragmentManager().getBackStackEntryCount());
boolean isPop = false;
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
isPop = true;
getChildFragmentManager().popBackStack();
}
return isPop;
}
}
Step3: Now here I am considering for one fragment only hoping that rest can be handled by you in same fashion. Defining container Fragment class. Each tab will have specific container. Say TalkContainerFragment.java for first tab
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import app.drugs.talksooner.R;
import app.drugs.talksooner.Talk;
import app.drugs.talksooner.utils.BaseContainerFragment;
public class TalkContainerFragment extends BaseContainerFragment {
private boolean mIsViewInited;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("test", "tab 1 oncreateview");
return inflater.inflate(R.layout.container_fragment, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("test", "tab 1 container on activity created");
if (!mIsViewInited) {
mIsViewInited = true;
initView();
}
}
private void initView() {
Log.e("test", "tab 1 init view");
replaceFragment(new Talk(), false);
}
}
It's xml file. "container_fragment.xml" this xml container contains frameLayout. we will use this id to replace different Fragments.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
Your main class. "Talk.java"
public class Talk extends Fragment {
/** Define global variables over here */
//private ProgressDialog pDialog;
StaticApiList sal;
TalkModelAll tma;
JSONObject myJasonObject = null;
private ListView lv;
private ArrayList<TalkModelAll> m_ArrayList = null;
//ArrayList<String> stringArrayList = new ArrayList<String>();
TalkArrayAdapter taa;
Set<String> uniqueValues = new HashSet<String>();
TextView rowTextView = null;
boolean vivek = false;
int postid;
String title;
String thumsrc;
String largeimg;
String excert;
String description;
String cat;
String myUrl;
String jsonString;
int mCurCheckPosition;
String check_state = null;
String ccc;
LinearLayout myLinearLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.talk, container, false);
Button btn = (Button) rootView.findViewById(R.id.your_btn_id);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Here TalkDetail is name of class that needs to open
TalkDetail fragment = new TalkDetail();
// if U need to pass some data
Bundle bundle = new Bundle();
bundle.putString("title", m_ArrayList.get(arg2).title);
bundle.putString("largeimg", m_ArrayList.get(arg2).largeimg);
bundle.putString("excert", m_ArrayList.get(arg2).excert);
bundle.putString("description", m_ArrayList.get(arg2).description);
bundle.putString("cat", m_ArrayList.get(arg2).cat);
//bundle.putInt("postid", m_ArrayList.get(arg2).postid);
fragment.setArguments(bundle);
((BaseContainerFragment)getParentFragment()).replaceFragment(fragment, true);
}
});
return rootView;
}
}
That's it. You are good to go. The whole magic lies in calling R.id. instead of R.layout.
Cheers!

Categories

Resources