Access Fragment Layout Objects From Java Not Working - android

I'm trying to access from my code to listview and a textview, I read a lot of posts on the internet but I didn't find anything that works for me.
I have a blank fragment like this:
public class MyFrag extends Fragment {
//My Code
List<String> Names = new ArrayList<String>();
List<Double> Prices = new ArrayList<Double>();
List<String> Dates = new ArrayList<String>();
List<String> Hours = new ArrayList<String>();
List<String> iDs = new ArrayList<String>();
Double TotalMy = 0.00;
SimpleAdapter adapter;
public MyFrag() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//RelativeLayout rootView = (RelativeLayout) inflater.inflate(R.layout.fragment_my_frag, container, false);
//View view = inflater.inflate(R.layout.fragment_my_frag, container, false);
LayoutInflater lf = getActivity().getLayoutInflater();
View view = lf.inflate(R.layout.fragment_my_frag, container, false);
ListView listview = (ListView) getView().findViewById(R.id.list);
TextView textTotalMy = (TextView) getView().findViewById(R.id.textTotalMy1);
return view;
}
But when I open the fragment the application crashes.
Please help me

I solved it by creating the same Relative Layout programmatically

Related

Can not dipslay textviews on top of the screen

Hello ladies and gentlmen,
Though I can receive data (reservations) on my screen, however I can not display some textviews on the screen.
public class Res extends Fragment {
ListView lview;
ListAdapter adapter;
ProgressDialog loading;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reslist, container, false);
lview = view.findViewById(R.id.lview);
getReservations();
return view;
here If I inflate fragment_reservation instead of reslist , I can display textviews,but I can display listview.
and here my listadapter code;
adapter = new SimpleAdapter(getActivity(),list,R.layout.fragment_reservations,
new String[]{"name", "email", "phone", "arriving", "departure", "pax", "roomtype", "rate"}, new int[]{R.id.textView, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5, R.id.textView6, R.id.textView7, R.id.textView8});
lview.setAdapter(adapter);
loading.dismiss();
maybe problem about getActivty() and I need to use different method calling..
Thanks in advance...
here the photo of the screen
SCREEEN
I think problem is with parameter "list"(ArrayList) while creating SimpleAdapter object. Here is my code to do the same thing, it is working fine.
public class BlankFrag extends Fragment {
private View v;
private ListView listView;
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState){
v = inflater.inflate(R.layout.blank_fragment, container, false);
listView = v.findViewById(R.id.dummy_list);
ArrayList<HashMap<String, String>> list = new ArrayList<>();
//populating dummy content
for(int i=0; i<5; i++){
HashMap<String, String> temp = new HashMap<>();
temp.put("name", "name"+Integer.toString(i));
temp.put("sname", "sname"+Integer.toString(i));
list.add(temp);
}
SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.single_item, new String[]{"name", "sname"}, new int[]{R.id.name, R.id.sname});
listView.setAdapter(adapter);
return v;
}
}

Autocomplete Text is not working in android fragment

I want to set autocomplete text in my application. The suggestion values will fetch from the database. But for a trial I made a string array for the suggestion values. It works fine when I implement it on an activity but not working in fragment.
In place of getActivity function I tried:
1) getContext()
2) this.getActivity()
3) (Search_Bus)getActivity()
but none work...
it's giving me error:
Attempt to invoke virtual method on a null object reference
Here's my code...
public class Bus_Schedule_tab3 extends Fragment {
AutoCompleteTextView autoCompleteTextView;
ArrayList<String> array;
ArrayAdapter<String> adapter;
String[] stop_names;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bus_schedule_tab3, container, false);
stop_names = new String[] {"usa","china","russia","bangladesh"};
autoCompleteTextView = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView2);
adapter = new ArrayAdapter<String>(getActivity(),R.layout.support_simple_spinner_dropdown_item,stop_names);
autoCompleteTextView.setAdapter(adapter);
return rootView;
}
Its working for me...You missed to set threshold
AutoCompleteTextView autoCompleteTextView;
ArrayAdapter<String> adapter;
String[] stop_names;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test, container, false);
stop_names = new String[]{"usa", "china", "russia", "bangladesh"};
autoCompleteTextView = rootView.findViewById(R.id.autoCompleteTextView2);
adapter = new ArrayAdapter<>(getActivity(), R.layout.support_simple_spinner_dropdown_item, stop_names);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.setAdapter(adapter);
return rootView;
}
Set in your xml file android:completionThreshold.
<AutoCompleteTextView
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:layout_weight="1"/>

ListView not displaying data in fragment using ArrayAdapter

i have a created a fragment in which i want to display listview but my fragment is dislplayed wtihout the listview nor i am getting an error
i have read many solutions to this question but i can't find the right one.
a small help would be great.thank you!
public class ServicesFragment extends Fragment {
public TextView servicesName;
ListView listView;
String[] servicesNameArray;
int[] serviceImages = {
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera,
R.drawable.ic_menu_camera
};
public ServicesFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.services_fragment, container, false);
listView = (ListView) view.findViewById(R.id.listView_services);
ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);
listView.setAdapter(adapter);
Resources resources = getResources();
resources.getStringArray(R.array.servicesName);
return view;
}
public class ServicesAdapter extends ArrayAdapter<String> {
Context c;
int[] servicesImageArray;
String[] servicesNameArray;
public ServicesAdapter(Context context, String[] servicesNameArray, int[] serviceImages) {
super(context, R.layout.services_layout, R.id.listView_services);
this.c = context;
this.servicesImageArray = serviceImages;
this.servicesNameArray = servicesNameArray;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.services_layout, parent, false);
ImageView serviceImages = (ImageView) view.findViewById(R.id.image_of_services);
TextView servicesName = (TextView) view.findViewById(R.id.name_of_services);
serviceImages.setImageResource(servicesImageArray[position]);
servicesName.setText(servicesNameArray[position]);
return view;}}}
You are not assigning value to the string array: use
servicesNameArray = getResources().getStringArray(R.array.servicesName);
and instead of :
ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);
use the activity context:
ServicesAdapter adapter = new ServicesAdapter(this.getActivity(), servicesNameArray, serviceImages);
you need to notify you adapter about your data set size:
super(context, R.layout.services_layout, servicesNameArray); //change here
I think this issue cause you dont pass data in correct way
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.services_fragment, container, false);
Resources resources = getResources();
servicesNameArray = resources.getStringArray(R.array.servicesName);
listView = (ListView) view.findViewById(R.id.listView_services);
ServicesAdapter adapter = new ServicesAdapter(getContext(), servicesNameArray, serviceImages);
listView.setAdapter(adapter);
return view;
}

spinner won't populate from my array [duplicate]

This question already exists:
Spinner will not populate and won't set arrayadapter
Closed 8 years ago.
My app redirects me to a different screen from my home with the tap of a button. Once, i reach my city screen my spinner is visible. But it doesn't contain any elements from my array. Nothing force closes or anything, but it doesn't work.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_city, container, false);
String[] test = {"test1", "test2"};
Spinner spin = (Spinner) v.findViewById(R.id.countries);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, test);
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapt);
return v;
}
You need to have the below.
public static PlaceHolderFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_city, container, false);
String[] test = {"test1", "test2"};
Spinner spin = (Spinner) v.findViewById(R.id.countries);
ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, test);
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapt);
return v;
}
}
Also use getActivity() to get the context of hosting activity.

android refresh UI of fragment kept ina view pager

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
logger = LogWrapper.getInstance();
logger.debugLog(TAG+"onCreateView", "On create View Start");
parentActivity = (CoreWorkFlowActivity) getActivity();
mView = inflater.inflate(R.layout.summary_layout, container, false);
this.inflater = inflater;
activateSystem = (Button)mView.findViewById(R.id.activate_system);
ivretestAll = (Button)mView.findViewById(R.id.ivretest);
activateSystem.setOnClickListener(this);
ivretestAll.setOnClickListener(this);
explistView = (ExpandableListView)mView.findViewById(R.id.explistview);
explistView.setGroupIndicator(null);
hiddenlayout = (RelativeLayout)mView.findViewById(R.id.hiddenLayout);
buttonlayout = (RelativeLayout)mView.findViewById(R.id.buttonlayout);
prepareListData();
explistAdapter = new CustomExpandableListAdapter(parentActivity, listDataHeader, listDataChild);
// setting list adapter
explistView.setAdapter(explistAdapter);
explistAdapter.notifyDataSetChanged();
return mView;}
when i am navigating through a viewpager the fragment's UI is not refreshed .Please give me a solution

Categories

Resources