I have an fragment on activity, with an listview element on it. Can't add data to listview. Can someone tell me what is wrong?
public class MainActivity extends FragmentActivity {
public String[] listS ={"item1","item2","item3"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MainFragment f = new MainFragment();
ft.replace(R.id.fragcontainer, f);
ft.commit();
//here the problem occurs
ListView lv = (ListView)findViewById(R.id.listf);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Fragment's class:
public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis){
return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
There's a number of problems with this method, but the root of it is that the FragmentTransaction replace() method that adds the fragment that, I assume, contains the ListView you're trying to find is an asynchronous process. The fragment isn't added to the Activity's view hierarchy immediately but some time later.
If you want to add things to a ListView in a Fragment, why not do it from within the MainFragment subclass' onCreateView() method instead? That's how it's intended to work.
EDIT: A typical onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
ListView lv = (ListView) view.findViewById(R.id.listf);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));
return view;
}
When u add code for define your class,
public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
ListView listView = (ListView) view.findViewById(R.id.list);
return view;
}
}
The problem is that you are asking to the activity for that ListView
"this."findViewById(...
You can ask the fragment for the view, or managing the content of the listView inside your Fragment class.
Please use the list view in the design windows as below. I faced the exact problem and now i could able to find the view in source code.
The main issue is IDE is appending "android" infront of ID
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
Related
I am trying to define a listview with a corresponding Adapter to add string to the listview. Howevewr, my listview comes as being null. The code is as below:-
public class CategoryFragment extends Fragment {
....
....
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView listView = (ListView) getActivity().findViewById(R.id.cat_vacancies);
listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_2);
listView.setAdapter(listAdapter);
....
}
}
What have I done wrong at this point?
my fragment xml is as follows( I have only added the relevant portions which I think may help in diagnosing the problem)
<RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/cat_vacancies">
</ListView>
</RelativeLayout>
P.S. This is, as far as I know, a silly mistake, but I have not yet completely grasped android completely, so excuse my problems.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.[layout id], container, false);
ListView listView = (ListView) view.findViewById(R.id.cat_vacancies);
retrun view;
}
Try whit that way
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.yourFragmentXml, container, false);
ListView listView = (ListView) view.findViewById(R.id.cat_vacancies);
listAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_2);
listView.setAdapter(listAdapter);
....
}
Hi I'm new to android and was trying to dynamically add content to a Fragment.
I've two Fragments inside an Activity. The first point to the second like this:
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_welcome,
container, false);
Button searchActivityBtn = (Button) rootView.findViewById(R.id.SearchActivityBtn);
searchActivityBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("##SecondActivity", "search btn click");
getFragmentManager().beginTransaction()
.replace(R.id.welcome_fragment, new searchAnnouncementsFragment()).addToBackStack(null).commit();
}
});
return rootView;
}
}
the second Fragment class is
public static class searchAnnouncementsFragment extends Fragment {
public searchAnnouncementsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search,
container, false);
return rootView;
}
//public void onStart(){
// super.onStart();
// RelativeLayout root = (RelativeLayout) getActivity().findViewById(R.id.annunciContainer);
// Log.d("##onstart", "root:" + (root == null));
//}
}
As you can see i tried to do something in onStart method but I've always a bunch of errors, the R.id.annunciContainer is the id of the fragment_search.xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id ="#+id/annunciContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white" >
What I'm trying to achieve is to dynamically add elements like other FrameLayout to my Fragment ( represented by the above RelativeLayout ).
Any thought?
You didn't mention the name of the activity, lets say the name as Activity1,
First create a linear layout or frame layout in the Activity1
Second add the following lines of code to your Activity1 for inflating PlaceholderFragment into your layout in Activity1
PlaceholderFragment pf = new PlaceholderFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.annunciContainer, pf,"place_holder");
ft.commit();
For inflating searchAnnouncementsFragment replace PlaceholderFragment with searchAnnouncementsFragment.
What I was searching for are ListView
Basically the situation is like this: I have an Activity, which displays a three Fragments within a FragmentPagerAdapter, and within one of these Fragment, I nest another Fragment. I added the fragments like this:
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(rootView.getId(), new MainPageFragment(), "lpt2");
fragmentTransaction.commit();
When I try to call getParentFragment() inside of MainPageFragment(), it returns null.
How can that be? I checked multiple posts here on stackoverflow as well as on other blogs, and they all came to the conclusion that if you nest one fragment within another one, calling getParentFragment should return its parent fragment. Returning null means, that the parent is an activity as stated here. Which is not the case. Please tell me I'm making some kind of silly mistakes :\
This is the Fragment that is a child to the main Activity
public class MainPage extends Fragment {
private FragmentTransaction fragmentTransaction;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
global_var = new Global();
rootView = inflater.inflate(R.layout.main_page, container, false);
return rootView;
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState) {
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(rootView.getId(), new MainPageFragment(), "lpt2");
fragmentTransaction.commit();
}
}
This is the Fragment which is a child to the Main Page Fragment
public class MainPageFragment extends Fragment{
private View rootView;
private Fragment parentFragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.main_page_list, container, false);
if (getParentFragment() == null) Log.i("damn", "it");
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
}
Please tell me if I need to provide more code, I extracted things like ListView and so on because I thought it would be irrelevant.
Edit: I'm using android.support.v4.app
Check that you have pass FragmentManager reference to all of your Fragments from your FragmentPagerAdapter and then use getParentFragment(). May be this will help you.
I have an activity with 3 fragments; a header; a body; a footer (same point as in HTML). The bodyfragment contains three buttons which each should replace the middle fragment (body; itself) with another one, but I can't figure out how to work FragmentManager and FragmentTransition in here. I can't seem to find any coherency in other peoples questions on here with regards to the way others implement their fragments. It seems everyone has their own methods, or just doesn't include the full code in their threads.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.test_frag);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
TestFragment.java
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_frag, container, false);
}
}
BodyFragment.java
public class BodyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.body_frag, container, false);
}
}
Fragment in XML
<fragment
android:id="#+id/bodyfragment"
android:name="com.example.test.BodyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="#layout/body_frag" />
BodyFragment layout in XML (button x3)
<Button
android:id="#+id/bsettings"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/bgames"
android:layout_alignBottom="#+id/bgames"
android:layout_toRightOf="#+id/bgames"
android:text="SETTINGS" />
You use the FragmentManager like this:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, AnotherFragment.newInstance()); // newInstance() is a static factory method.
transaction.commit();
This code would replace the Fragment which sits in the View with the id R.id.bodyfragment with a new Instance of another Fragment.
EDIT:
To create a new instance of another Fragment a static factory method is supposed to be used. You would implement them in your Fragment like this:
public class AnotherFragment extends Fragment {
public static AnotherFragment newInstance() {
AnotherFragment fragment = new AnotherFragment();
...
// do some initial setup if needed, for example Listener etc
...
return fragment;
}
...
}
I am trying to implement Fragments in my Project using the https://github.com/JakeWharton/Android-ViewPagerIndicator plugin.
My Fragment
public final class NearByFragment extends Fragment {
private static String TAG = "bMobile";
public static NearByFragment newInstance(String content) {
NearByFragment fragment = new NearByFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment_search_nearby, null);
}
}
Now I want to execute some code like start start a new thread and download a JSON from the server and then assign a list view from the content I download. In fragments we are assigning views in onCreateView. so where should I write the
listview = (ListView) findViewById(R.id.list_items);
((TextView) findViewById(R.id.title_text))
.setText(R.string.description_blocked);
and other code to generate the Fragment view ?
You can search within the view that you just inflated:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
View v = inflater.inflate(R.layout.fragment_search_nearby, null);
listview = (ListView)v.findViewById(R.id.list_items);
((TextView)v.findViewById(R.id.title_text))
.setText(R.string.description_blocked);
return v;
}
You can also use the Fragment.getView() function elsewhere in your code and call that view's findViewById() member.
You can use getActivity().findByView() from the Fragment if you want to have access to the layout elements. Alternatively, you can just put the findByView() call in the main Activity.