I am trying to show a listview in a fragment layout; for that i have used custom adapter.Note that on a button click, fragment is changed. But on that button click application crashes, I don't know what is actual problem please help.
This is my Fragment Class:
public class Fragment1 extends Fragment {
public void Fragment1()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fragment1, container, false);
//return inflater.inflate(R.layout.fragment_fragment1, container, false);
ListView listView = (ListView)rootView.findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
return rootView;
}
class CustomAdapter extends BaseAdapter
{
int img_arr[] = {R.drawable.emmastone,
R.drawable.chloegracemoretz,
R.drawable.salmankhan,
R.drawable.emilia,
R.drawable.kitharington,
R.drawable.scarlettjohansson,
R.drawable.sophie,
R.drawable.deepika,
R.drawable.leonardodicaprio};
String name_arr[] = {"Emma", "Chloe", "Salman", "Emilia", "Kit", "Scarlet", "Sophie", "Deepika", "Leonardo" };
String msg_arr[] = {"Free at 8pm?", "I miss you <3", "Mota ho raha hun kia karon?", "Not in mood", "Danny or yagrit?", "Where are you?", "got engaged <3", "Give my money", "50 is enough" };
#Override
public int getCount() {
return img_arr.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.chats,null);
ImageView img = (ImageView)convertView.findViewById(R.id.imageView);
TextView name = (TextView)convertView.findViewById(R.id.textView_name);
TextView msg = (TextView)convertView.findViewById(R.id.textView_msg);
img.setImageResource(img_arr[position]);
name.setText(name_arr[position]);
msg.setText(msg_arr[position]);
return convertView;
}
}
}
This is in logcat:
0-21 23:52:21.581 3040-3040/com.example.farhan.a13_f_8310_lab_10_oct_fragments E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.farhan.a13_f_8310_lab_10_oct_fragments, PID: 3040
java.lang.NoSuchMethodError: No virtual method getLayoutInflater()Landroid/view/LayoutInflater; in class
Lcom/example/farhan/a13_f_8310_lab_10_oct_fragments/Fragment1; or its super classes (declaration of 'com.example.farhan.a13_f_8310_lab_10_oct_fragments.Fragment1' appears in /data/app/com.example.farhan.a13_f_8310_lab_10_oct_fragments-1/split_lib_slice_1_apk.apk)
at com.example.farhan.a13_f_8310_lab_10_oct_fragments.Fragment1$CustomAdapter.getView(Fragment1.java:91)
Related
I have four Fragments(tab swipe stuff). For example I created a Toast in my Fragment #2, but this toast also comes up in my Fragment #4 when I open it.
What is missing here .
this is my fragment #2
public class A extends Fragment {
View view ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.xxx_updates_layout, container, false);
//Custom Toast
LayoutInflater inflaterToast = getActivity().getLayoutInflater();
View layout = inflaterToast.inflate(R.layout.d_toast_d,
(ViewGroup) view.findViewById(R.id.toast_layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_people);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Ringtone Bank");
Toast toast = new Toast(getActivity().getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
return view;
} //onCreate
}
This is my fragment #4
public class XXXSentFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.xxx_sent_layout,null);
}
}
My Fragment Pager Adapter
public class XXXTabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 4 ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.xxx_tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new XXXPrimaryFragment();
case 1 : return new XXXSocialFragment();
case 2 : return new XXXUpdatesFragment();
case 3 : return new XXXFOUR();
}
return null;
}
#Override
public int getCount() {
return int_items;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
case 3 :
return "Four";
}
return null;
}
}
}
The thing is that View Page loads 3 fragments at a time (the one being shown, the one on the left and the one on the right). Hence the onCreateView for all of them get called at once which is why the toast from another fragment appears.Put a log statement in the onCreateView of all your fragments to verify this and to get a better understanding of how this works.
If your use case is to show a toast when the user swipes or selects a certain fragment (In your case XXXUpdatesFragment()), one way to go about this is to hold references to your fragments in your FragmentPagerAdapter and then implement the onPageChangeListener on the viewPager, when the page changes to 2, use the reference to call a method inside XXXUpdatesFragment() which will show a toast.
public class XXXUpdatesFragment extends Fragment {
View view ;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
view = inflater.inflate(R.layout.xxx_updates_layout, container, false);
return view;
}
public void showToast() {
//Custom Toast
LayoutInflater inflaterToast = getActivity().getLayoutInflater();
View layout = inflaterToast.inflate(R.layout.d_toast_d,
(ViewGroup) view.findViewById(R.id.toast_layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_people);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Ringtone Bank");
Toast toast = new Toast(getActivity().getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
}
I am implementing array list adopter uisng "simple_list_item_2" that is inbuilt but I get error. it says " getview from fragment cannot be applied" and it also cannot resolve "get" symbol from get.(position)
public class AboutFragment extends Fragment {
ListView listView2;
String[] items = {"Friendly Map", "Inc"};
public AboutFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_about, container, false);
listView2 = (ListView)v.findViewById(R.id.listView2);
ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_2, items);
listView2.setAdapter(arrayAdapter);
return v;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
String[] entry = listView2.get(position);
return view;
}
}
Try this.
public class AboutFragment extends Fragment {
ListView listView2;
String[] items = {"Friendly Map", "Inc"};
public AboutFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_about, container, false);
listView2 = (ListView) v.findViewById(R.id.listView2);
ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_2, items);
listView2.setAdapter(arrayAdapter);
return v;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
String entry = listView2.get(position);
return view;
}
}
I have used viewpager which displays view with an image and radiobutton below. Each swipe screen is not a fragment, they are layouts which just swipe the same view with different imageview. The problem is that, I am unable to deselect the radio button which is in left and right side of the current view after i select the radio button of the current screen. If I return POSITION_NONE in getItemPosition(), it refresh the screen and radio button also deselected but the view is flickered. If i am able to call instantiateItem(), my problem is solved . But this method is called only when view is destroyed based on the setOffsetScreenPageLimit(). How can I achieved such requirements, if view pager cannot help?
Fragment:
public class AnswerImageDialogFragment extends DialogFragment implements ViewPager.OnPageChangeListener {
//member declaration
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strQuestion = null;
View rootView = inflater.inflate(R.layout.activity_viewpager_demo, container,
false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
intro_images = (ViewPager) rootView.findViewById(R.id.pager_introduction);
pager_indicator = (LinearLayout) rootView.findViewById(R.id.viewPagerCountDots);
//do some stuff
//set the adapter
mAdapter = new ViewPagerAdapter(getContext(), map);
intro_images.setAdapter(mAdapter);
intro_images.setCurrentItem(clickPosition);
intro_images.setOnPageChangeListener(this);
setUiPageViewController();
bus = EventBus.getDefault();
bus.register(this);
return rootView;
}
//other method
}
PagerAdapter:
public class ViewPagerAdapter extends PagerAdapter {
// member declaration
public ViewPagerAdapter(Context mContext, HashMap<String, Object> map) {
//other initialization
}
#Override
public int getCount() {
return optionImages.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.pager_item, container, false);
final ImageView ivOption = (ImageView) itemView.findViewById(R.id.answerId); // this is used as radio button in this case
/*The code snippet is to select the answer option based on whether answer is choosen or not.
If choosen, select as default in the pop up answer.
*/
if (null != ans) {
Iterator iterator = ans.iterator();
if (ans.size() > 0) {
int value = (int) iterator.next();
if (value == position) {
ivOption.setImageResource(R.drawable.ic_radio_button_tick);
} else {
ivOption.setImageResource(R.drawable.ic_radio_button_nontick);
}
}
}
p = position;
/*The code snippet is to change the answer value based on the user selection.
This means if user choosen another answer then clear the earlier choosen answer
*/
ivOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff
}
});
txtQuestion.setText(question);
txtOption.setText(optionsList.get(position));
imageView.setParseFile(optionImages.get(position));
imageView.loadInBackground();
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
LinearLayout ll = (LinearLayout) object;
/*RelativeLayout ivOption = (RelativeLayout) ll.getChildAt(2);
ImageView iv = (ImageView) ivOption.getChildAt(1);
iv.setImageResource(R.drawable.ic_radio_button_tick);*/
container.removeView(ll);
}
#Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
public void refresh(ArrayList object) {
this.object = new ArrayList();
this.object.addAll(object);
this.notifyDataSetChanged();
}
public class AdapterListView extends BaseAdapter {
Context context;
ArrayList<String> brands;
public AdapterListView(ArrayList<String> brands, Context context) {
this.brands = brands;
this.context = context;
}
#Override
public int getCount() {
return brands.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view= LayoutInflater.from(context).inflate(R.layout.activity_listview,null);
ListView lv=(ListView)view.findViewById(R.id.mobile_list);
lv.setText(brands.get(i));
return view;
}
}
setText has error and displaying (cannot resolve method,settext(java.lag.string))
my message fragment class is given bellow am actually trying to set a listview
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_messages, container, false);
brands.add("microsoft");
brands.add("ios" );
brands.add("android");
AdapterListView adapter = new AdapterListView(brands,getActivity());
ListView listView = (ListView)rootView.findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return rootView;
}
i need to print the list brands.is thr any error in the way i set the listview
setText has error and displaying (cannot resolve
method,settext(java.lag.string))
Because setText method is not defined in ListView class.
Use TextView,EditText,Button like views for using setText method
instead of
ListView lv=(ListView)view.findViewById(R.id.mobile_list);
lv.setText(brands.get(i));
you have to take TextView in your R.layout.activity_listview
and than code should be
TextView tv=(TextView)view.findViewById(R.id.textViewId);
tv.setText(brands.get(i));
As the title I want to create a listview with custom row in Fragment. My code below.
Fragment class
public class PhotosFragment extends Fragment{
public PhotosFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_photos, container, false);
ArrayList<ListviewContactItem> listContact = GetlistContact();
ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
lv.setAdapter(new ListviewContactAdapter(getActivity(), listContact));
return rootView;
}
private ArrayList<ListviewContactItem> GetlistContact(){
ArrayList<ListviewContactItem> contactlist = new ArrayList<ListviewContactItem>();
ListviewContactItem contact = new ListviewContactItem();
contact.SetName("Topher");
contact.SetPhone("01213113568");
contactlist.add(contact);
contact = new ListviewContactItem();
contact.SetName("Jean");
contact.SetPhone("01213869102");
contactlist.add(contact);
contact = new ListviewContactItem();
contact.SetName("Andrew");
contact.SetPhone("01213123985");
contactlist.add(contact);
return contactlist;
}
}
Adapter class
public class ListviewContactAdapter extends BaseAdapter{
private static ArrayList<ListviewContactItem> listContact;
private LayoutInflater mInflater;
public ListviewContactAdapter(Context photosFragment, ArrayList<ListviewContactItem> results){
listContact = results;
mInflater = LayoutInflater.from(photosFragment);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listContact.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return listContact.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.contact_item, null);
holder = new ViewHolder();
holder.txtname = (TextView) convertView.findViewById(R.id.lv_contact_item_name);
holder.txtphone = (TextView) convertView.findViewById(R.id.lv_contact_item_phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtname.setText(listContact.get(position).GetName());
holder.txtphone.setText(listContact.get(position).GetPhone());
return convertView;
}
static class ViewHolder{
TextView txtname, txtphone;
}
}
But when I run the app that display no thing. Could anyone tell me what wrong here and how can I fix it?
I guess your app crashes because of NullPointerException.
Change this
ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
to
ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);
assuming listview belongs to the fragment layout.
The rest of the code looks alright
Edit:
Well since you said it is not working i tried it myself
Please use ListFragment. Otherwise, it won't work.
EDIT 1:
Then you'll only need setListAdapter() and getListView().
you need to give:
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
inside fragment.
The inflate() method takes three parameters:
The id of a layout XML file (inside R.layout),
A parent ViewGroup into which the fragment's View is to be inserted,
A third boolean telling whether the fragment's View as inflated from
the layout XML file should be inserted into the parent ViewGroup.
In this case we pass false because the View will be attached to the
parent ViewGroup elsewhere, by some of the Android code we call (in
other words, behind our backs). When you pass false as last parameter
to inflate(), the parent ViewGroup is still used for layout
calculations of the inflated View, so you cannot pass null as parent
ViewGroup .
View rootView = inflater.inflate(R.layout.fragment_photos, container, false);
So, You need to call rootView in here
ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);
Instead:
public class PhotosFragment extends Fragment
You can use:
public class PhotosFragment extends ListFragment
It change the methods
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<ListviewContactItem> listContact = GetlistContact();
setAdapter(new ListviewContactAdapter(getActivity(), listContact));
}
onActivityCreated is void and you didn't need to return a view like in onCreateView
You can see an example here