findViewById not recognised in ListFragment [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
findViewById in fragment android
I am currently working on android project and trying to get fragments working. The fragment part is working but I'm trying to control UI components, on a standard activity I can use something like
TextView txtMyTextBox = (TextView)findViewById(R.id.my_text_box)
I am extending the class by ListFragment but when why I try the same code as above I get the following error
The method findViewById(int) is undefined
Why doesn't this work. Thanks for any help you can provide.

That method is not defined in the Fragment class which is where I assume you're calling it. You have to use it on the View that the Fragment is showing. This is the same view that you return in onCreateView() and you can retrieve it by getView(). Thus, TextView txtMyTextBox = (TextView)getView().findViewById(R.id.my_text_box) should work.

When you use fragments, you should override the onCreateView method.
Here is an exemple :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.YOUR_FRAGMENT_LAYOUT, null);
TextView txtMyTextBox = (TextView)v.findViewById(R.id.my_text_box)
return v;
}

Related

How to set/change value while onCreate under TabLayout first Tab

i have try a lot of example or tutorial on tablayout, all work fine
(https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/, http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/)
but now i encounter 1 problem, which is i dont know how to set/change the TextView/ImageView while onCreate/onLoad
so far, what i able to do is, write all my code under onTabSelected, but this is not my solution because i cant show empty or static values on the 1st tab, then have to wait until click or slide again then only load the real data.
i'm sure it have a way or solution for my problem, can anyone share it to me (code/website)
or in order to have this Tab feature with viewpager contain recyclerview, Tablayout is not the right way to code, perhaps it have another more correct way to code.
Do these changes inside of onCreateView of fragment that you want
Example:
//Our class extending fragment
public class Tab1 extends Fragment {
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Change R.layout.tab1 in you classes
View view = inflater.inflate(R.layout.tab1, container, false);
TextView textView = (TextView) view.findViewById(R.id.you_textview_id);
textView .setText("Your text to show");
//Returning the layout file after inflating
return view;
}
}
The TextView need to be previous declared in tab.xml that you're inflating, in our example, tab1.

Android app programming- not sure why findViewByID does the following:

This code works:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
Intent intent = getActivity().getIntent();
String weather = intent.getStringExtra(Intent.EXTRA_TEXT);
if(weather != null) {
TextView textViewWeather=(TextView)rootView.findViewById(R.id.weatherText);
textViewWeather.setText(weather);
BUT if you replace, in the line
(TextView)rootView.findViewById(R.id.weatherText);
rootView by findView() or getActivity, it returns null so the next line creates a run time fatal error which halts the program.
I don't understand why. I thought that the context is the activity?! I'm confused,and can't find a good answer via android developer,so here I am...
Thanks in advance to whoever helps!!
when you call :
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
TextView textViewWeather=(TextView)rootView.findViewById(R.id.weatherText);
the rootView now contains the views that are defined in fargment_detail.xml .
and weatherText is one of them.
but when you call getActivity().findViewById() or findViewById() you want to find the weatherText inside xml file of activity and there is no weatherText there so it will retun null
This line gets the element with id weatherText from its rootView. Which is nothing but your fragment xml file
TextView textViewWeather=(TextView)rootView.findViewById(R.id.weatherText);
Now if you are replacing above code with below
TextView textViewWeather=(TextView)getActivity().findViewById(R.id.weatherText);
now the parent Activity in which this fragment is hosted which has TextView and id weatherText will be searched by framework. Which is obvious is not present at all.
If you remove the rootView you are calling it directly on the activity so those two suggestions are equal (and activity is indeed the context).
But the activity doesn't know about the layout yet, you just inflated it from xml but it has not been given to the activity yet, and therefore can't be found. You do that by returning it from the onCreateView call you are in in your example code.

What is the difference between getView() and getActivity()?

What is the difference between getView() and getActivity()?
I have used both methods but don't understand the basic difference even methodology of usage are also same in android:
ListView deliverItemList = (ListView) getView().findViewById(R.id.load_item_list);
ListView deliverItemList = (ListView) getActivity().findViewById(R.id.load_item_list);
I have assumed that getView() may produce NullPointerException, share your knowledge with me and which method is recommended?
getActivity() returns the Activity hosting the Fragment, while getView() returns the view you inflated and returned by onCreateView. The latter returns a value != null only after onCreateView returns
From android docs:
getActivity() returns the Activity this fragment is currently
associated with, and getView() returns the root view for the
fragment's layout (the one returned by onCreateView(LayoutInflater,
ViewGroup, Bundle)), if provided.
So, in your case, by the following line of code:
getView().findViewById(R.id.load_item_list);
you are searching for the view in your fragment, but using the following line of code:
getActivity().findViewById(R.id.load_item_list);
you are searching for the view in your activity hosting your fragment.
About your question of which one to use, it depends. If you are trying to inflate fragment, you need to inflate your xml in onCreateView, and using that inflated view you search for your views like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_layout, container, false);
ListView lv = (ListView)v.findViewById(R.id.view_id);
}

why we access like that getActivity().findViewById()

When a layout , which is defined for a Fragment and used inside setContentView() method and it has no relation with Activity .
then why we need Activity Reference to access it .
so I want to create a ListView , inside a Fragment . than i have to create it inside Activity layout or inside Fragment layout .
Hey Neil , i have a question because every time I have made a mistake because , i am performing this
TextView txt = ( TextView ) getActivity().findViewById(R.id.txt);
txt.setText("nayak nahi , khalnayak hun mein");
inside oncreateView() so it is creating problem but when ever i am implementing this inside
onActivityCreated() , it is working , so can you please tell me Why this is happening .
if you want to find the view by getActivity(), you should do that after onCreateView is called, because till onCreateView is called the view of fragment is null. So you can do:
#Override
public void onResume() {
super.onResume();
TextView txt = ( TextView ) getActivity().findViewById(R.id.txt);
txt.setText("nayak nahi , khalnayak hun mein");
}
You can define a ListView in a layout for your Fragment. Try something like this in your fragment to define a alternative layout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
_baseView = inflater.inflate(R.layout.yourlayout, null); //where base view is your upper most parent in your layout
return _baseView;
}
Then in your onActivityCreated you can use _baseView.findViewById('yourListView') to set your ListView. I don't know if this is best practice but it works perfectly for me.
In Fragment you should use its View but getActivity:
public View onCreateView(…){
View rootView = inflater.inflate(R.layout.listview_xml_file, container, false);
ListView ListNewsBelarus = (ListView) rootView.findViewById(R.id.listViewId);
return rootView;
}

setText in a ListFragment

I'm a beginner at Android programming and I was wondering if you could help me out.
I have a ListFragment:
public class AdvertFrag extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
that I'm populating from a database using a loaderManager. This works fine and displays details of adverts, but I would like to do some calculations (work out distance via longitude and latitude) and then have the distance to the shops location displayed on each AdvertFrag.
I have looked into using setText() on a TextView in a fragment and have read you should allow the onCreateView method to be called first so a view is present. I have tried the below code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
view = inflater.inflate(R.layout.row, container, false);
if(view == null)
{
Log.i("TEST","null");
}
else
{
distance = (TextView) view.findViewById(R.id.distance);
distance.setText("the distance will go here");
}
return view;
}
But get the error that "Your content must have a ListView whose id attribute is android.R.id.list. Allowing the OnCreateView to just call:
return super.onCreateView(inflater, container, savedInstanceState);
works fine.
So my question is, what am I doing wrong? and how do I get setText working in a ListActivity? Or am i going at it in totally the wrong way?
Addition info if its useful:
My row.xml, just has the text views that are populated by the loader and the additional textview for the distance.
I have a fragmentActivity that has the fragment in its XML, like so:
<fragment
...
android:name="com.example.goclub.AdvertFrag"
.../>
Like user113215 said onCreateView() of the ListFragment is not a place to change widgets inside the list items. You will have to create a custom CursorAdapter for that assuming that you are using one. You can find a nice tutorial about this here. Basically it extends a SimpleCursorAdapter and makes changes in the bindView method.

Categories

Resources