I have an problem when show data in fragment. I send data from another activity to fragment and show it but it's show. Here is my code.
Function in activity:
public void sendResultRoundtripDomestic(ArrayList<ItemSearch> listOutbound, ArrayList<ItemSearch> listInbound) {
listItemOutbound = ListItem.createSearchOnewayDomesticResult(listOutbound);
outboundFragment = new OutboundFragment(listItemOutbound, 0);
listItemInbound = ListItem.createSearchOnewayDomesticResult(listInbound);
inboundFragment = new InboundFragment(listItemInbound, 1);
setupViewPager(viewPager);
}
And here is my fragment I want to show
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rv = inflater.inflate(R.layout.fragment_outbound, container, false);
rvResultOutbound = (RecyclerView) rv.findViewById(R.id.roundtrip_outbound);
adapterOutbound = new OnewayAdapter(rv.getContext(), list);
rvResultOutbound.setAdapter(adapterOutbound);
rvResultOutbound.setLayoutManager(new LinearLayoutManager(rv.getContext(), LinearLayoutManager.VERTICAL, false));
return rv;
}
public OutboundFragment(ArrayList<ListItem> list, int position) {
this.list = list;
this.position = position;
}
Can you help me fix this?
Related
i have created a fragment in android that contain 2 functions, the startview() is called at the beginning when the fragment is called, then when user click a button childfragment() is called. and at that point it should have remove the view from the startview() and then replacing it with the newview from childfragment(), and thats what i thought it would do. But instead it did not remove the old view and place the newview on top of the old view. even thought i already place mContainer.removeAllViews(); it did not remove the old view. what am i missing? please help.
here is the code
#SuppressLint("ValidFragment")
public class FragmentSideContent extends android.app.Fragment {
//private OnFragmentInteractionListener mListener;
Context context;
View view,trueview;
ListView listView;
ListAdapter listAdapter;
private LayoutInflater mInflater;
private ViewGroup mContainer;
int[] image = {1,2,3};
String[] titleText = {"one","two","three","one","two","three","one","two","three","one","two","three"};
String[] subTitleText = {"one","two","three","one","two","three","one","two","three","one","two","three"};
public FragmentSideContent(Context context) {
this.context = context;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,#Nullable Bundle savedInstanceState) {
mInflater = inflater;
mContainer = container;
startview();
return trueview;
}
public void startview(){
view = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);
listView = (ListView) view.findViewById(R.id.noteList);
listAdapter = new ListAdapter(context,image,titleText,subTitleText);
listView.setAdapter(listAdapter);
trueview = view;
}
public void childfragment(){
mContainer.removeAllViews();
View newview = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);
Fragment childFragment = new FragmentList(context);
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.child_fragment_container, childFragment).commit();
mContainer.addView(newview);
mContainer.addView(trueview);
trueview = newview;
}
}
I recommend you put in your fragment layout some GroupView for do this.
fragment_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
in yout Fragment.java
View view;
Framelayout fContainer;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_layout, container, false);
return startView();
}
public View startview(){
fContainer = (Framelayout) view.findViewById(R.id.container);
listView = (ListView) view.findViewById(R.id.noteList);
listAdapter = new ListAdapter(context,image,titleText,subTitleText);
listView.setAdapter(listAdapter);
return view;
}
public void childfragment(){
fContainer.removeAllViews();
Fragment childFragment = new FragmentList(context);
FragmentTransaction transaction =
getChildFragmentManager().beginTransaction();
transaction.replace(R.id.container, childFragment).commit();
}
Now in your oncreateview of the other fragment (FragmentList) put the view that you want inflate.
FragmentList.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View newview = mInflater.inflate(R.layout.fragment_sidecontent,mContainer,false);
return newview;
}
I have a GridView in FragmentA containing all objects. When the user tap on each object, these objects are added into ListView of FragmentB.
Note that FragmentA and FragmentB are both in the same Activity, side by side.
I have tried using Bundle to send object from FragmentA to FragmentB but I was stucked as I have no idea how FragmentB is going to know that a new object has been added.
Bundle object is sent from ProductGridAdapter (under FragmentA) and receiver will be FragmentB where FragmentB will update the ListView for the latest objects added by FragmentA
Here is my code:
ProductGridAdapter.java
public class ProductGridAdapter extends ArrayAdapter<Product> {
private List<Product> productList;
private Context context;
public ProductGridAdapter(Context context, int resource,
List<Product> objects) {
super(context, resource, objects);
this.context = context;
this.productList = objects;
}
#Override
public int getCount() {
return ((null != productList) ?
productList.size() : 0);
}
#Override
public Product getItem(int position) {
return ((null != productList) ?
productList.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(null == view) {
view = layoutInflater.inflate(R.layout.grid_product, null);
}
final Product product = productList.get(position);
if(product != null) {
final CardView productGridLayout = (CardView) view.findViewById(R.id.product_gridlayout);
final TextView productName = (TextView) view.findViewById(R.id.product_name);
final ImageView productIcon = (ImageView) view.findViewById(R.id.product_icon);
productName.setText(product.getName());
productIcon.setImageDrawable(product.getDrawable());
productGridLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
bundle.putSerializable("product", product);
fragment.setArguments(bundle);
}
});
}
return view;
}
}
FragmentA.java
public class FragmentA extends Fragment {
private GridView gridView;
private ProductGridAdapter productAdapter;
public FragmentA() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_product_display, container, false);
gridView = (GridView) rootView.findViewById(R.id.gridview);
productAdapter = new ProductGridAdapter(getActivity(), R.layout.fragment_product_display, getProductList());
gridView.setAdapter(productAdapter);
gridView.destroyDrawingCache();
gridView.setVisibility(GridView.INVISIBLE);
gridView.setVisibility(GridView.VISIBLE);
return rootView;
}
}
FragmentB.java
public class FragmentB extends Fragment {
private ListView listView;
private PaymentListAdapter paymentAdapter;
private LinearLayout button;
private ArrayList<String> arr;
public FragmentB() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_payment, container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
paymentAdapter = new PaymentListAdapter(getActivity(), R.layout.fragment_payment, getProductList());
// button = (LinearLayout) rootView.findViewById(R.id.payment);
listView.setAdapter(paymentAdapter);
Bundle bundle = this.getArguments();
Product product = (Product) bundle.getSerializable("product");
return rootView;
}
you can have two common method in interface class in main activity, To update view in both fragment,When some changes happens in FragmentA you have to notify that changes to main activity then main activity should communicate to FragementB about that changes.try to do as like this,Hope this will help you.Happy Coding!
I want to Add Simple List View of some items under Fragment activity which is extending Fragment but not ListFragment.
Here is My code :
public class ProductListFragment extends Fragment {
String[] ibizzproducts=
{
"Attendance",
"GPS",
"CCTV"
"Website"
"Application"
};
ListView listView;
public static ProductListFragment newInstance(int sectionNumber) {
ProductListFragment fragment = new ProductListFragment();
Bundle args = new Bundle();
args.putInt(Constants.ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public ProductListFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i < ibizzproducts.length;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Products Here : " + ibizzproducts[i]);
aList.add(hm);
}
String[] from = { "Products Here" };
int[] to = { R.id.listView1};
View v = inflater.inflate(R.layout.fragment_product_list, container, false);
// ListView list = (ListView)v.findViewById(R.id.listView1);
listView = (ListView)v.findViewById(R.id.listView1);
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.fragment_product_list, from, to);
listView.setAdapter(adapter);
return v;
If I am using this in onCreateView(), thn its giving me error:
java.lang.IllegalStateException: android.widget.ListView is not a view that can be bounds by this SimpleAdapter
I just want to add simple ListView , so that if i select Product from the navigation Drawer, it should show me this simple List of product items.
please , help me with this.
Thanks
Here:
listView.setAdapter(adapter);
listView object is null because getActivity().findViewById is used to access ListView from fragment_product_list layout which is layout of Fragment.
Because list object of ListView is initialized from fragment_product_list which is returned from onCreateView method so used it to setAdapter for ListView:
list.setAdapter(adapter);
Change your code from listView.setAdapter(adapter); to list.setAdapter(adapter);
Keep your eyes open while copy-paste is being done
It is better:
for(int i = 0; i < ibizzproducts.length; i++) {
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Products Here : " + ibizzproducts[i]);
aList.add(hm);
}
i hope it will help you
public class FragmentShowAll extends Fragment {
ListView listViewMsg;
ArrayList<String> array_list=new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fragment_all, container, false);
array_list.add("hello");
array_list.add("hello");
array_list.add("hello");
array_list.add("hello");
array_list.add("hello");
listViewMsg = (ListView) v.findViewById(R.id.listViewMsg);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,array_list);
listViewMsg.setAdapter(adapter);
listViewMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//on click action
}
});//listview on click
return v;
}
}
I used fragment in my app and i'm using SQLite to save local data. But when I finished saving data, and I swipe the page, my listView is not refreshed with new data (Only showing old data). I have tried to provide a method notifyDataSetChanged() on my adapter, but it's not working.
My Base Adapter class :
public class LocalDataAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<LocalDataBean> data;
private static LayoutInflater inflater = null;
public LocalDataAdapter(Activity a, ArrayList<LocalDataBean> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public void setItem(ArrayList<LocalDataBean> data){
this.data = data;
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = inflater.inflate(R.layout.list_item, null);
TextView nama_konsumen = (TextView) v.findViewById(R.id.nama_konsumen);
TextView no_telp = (TextView) v.findViewById(R.id.no_telp);
TextView no_hp_cdma = (TextView) v.findViewById(R.id.no_hp_cdma);
TextView no_hp_gsm = (TextView) v.findViewById(R.id.no_hp_gsm);
LocalDataBean obj = (LocalDataBean) getItem(position);
nama_konsumen.setText(obj.getNamaKonsumen());
no_telp.setText(obj.getNoTelp());
no_hp_cdma.setText(obj.getNoCMDA());
no_hp_gsm.setText(obj.getNoGSM());
return v;
}
}
My fragment class :
public class LocalDataFragment extends Fragment {
View view;
Activity act;
SQLHandlerBean utilSql;
ArrayList<LocalDataBean> localdatabean = new ArrayList<LocalDataBean>();
LocalDataAdapter adapter;
ListView list;
public static final String TAG = LocalDataFragment.class.getSimpleName();
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.layout_local_data, null);
act = getActivity();
list = (ListView) view.findViewById(R.id.listViewLocalData);
utilSql = new SQLHandlerBean(this.act);
adapter = new LocalDataAdapter(act, localdatabean);
localdatabean = new ArrayList<LocalDataBean>();
list.setAdapter(adapter);
if (utilSql.ReadAllLocalData().size() < 1) {
Toast.makeText(act, "DATA EMPTY!", Toast.LENGTH_LONG).show();
} else {
localdatabean = utilSql.ReadAllLocalData();
Log.e(TAG, "TOTAL DATA : "+localdatabean.size());
adapter.setItem(localdatabean);
adapter.notifyDataSetChanged();
}
return view;
}
}
Is adapter.notifyDataSetChanged() placement correct?
No, the placement is not in the right place.
As you have placed the notifyDataSetChanged() inside of the onCreateView() method. It will be only invoked 1st time the fragment is launched.
Rather you can add a refresh button in your layout (or in you action bar). And along with the insertion/deletion method of the data, place the notifyDataSetChanged() at the bottom of the click event of that button.
By doing this user can refresh the page whenever they want.
And if you want to refresh the page by swipping the view then, SwipeRefreshLaoyout could be a perfect alternative.
You can check this blog.
I'm using ListFragment in my android project. I decided to make a listView, each listview item consist of music frequency imageView. I will try to animate the imageView of music frequency. Each listView has a button called play, once the play is clicked, the music frequency start to animate.
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmentmusiclist, container, false);
lv = (ListView) v.findViewById(R.id.list);
mp = new MediaPlayer();
mp = null;
Song s1 = new Song("Love Story",(float) 2.5, onMusicPlayListener);
Song s2 = new Song("Sad Story",(float) 1.0, onMusicPlayListener);
Song s3 = new Song("Breakup Story",(float) 3.5, onMusicPlayListener);
songs = new ArrayList<Song>();
songs.add(s1);
songs.add(s2);
songs.add(s3);
adapter = new MusicListAdapter(getActivity(),songs);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
OnClickListener onMusicPlayListener = new OnClickListener() {
#Override
public void onClick(View v) {
final int position = getListView().getPositionForView((LinearLayout)v.getParent());
animMusicFreq = (ImageView) v.findViewById(R.id.music_anim);
animMusicFreq.setBackgroundResource(R.anim.anim_music);
animMusicFreq.post(new Runnable() {
#Override
public void run() {
AnimationDrawable frameAnimation =
(AnimationDrawable) animMusicFreq.getBackground();
frameAnimation.start();
}
});
You are inflating your view inside onCreateView, but you don't pass it to the system. Instead you tell it to inflate its own (you return super). It doesn't make any sense.
You should return your v view like so:
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmentmusiclist, container, false);
// ...
return v;
}