getListView is missing - android

I have created a ListView to list all the activities but when I wanna call the method, I can't find getListView
My activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListActivity"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
and in my MainActivity.java seems like there's an error with
getListView
method, I've no idea why

You can use getListView() when working with ListActivity not the usual Activity. In your code you got it wrong and made the ListView with id "a special identifier to the view" ListActivity, so to access it in your code you simply
ListView lv = (ListView) findViewById(R.id.ListActivity);

Your activity must inherit from a ListActivity. Your class should start with
public class MyListAdapter extends ListActivity {
...
}
Then you can use the method
getListView()
This is the official documentation
Second solution
If you use a fragment, then you can access your ListView by using
ListView lv = (ListView) rootView.findViewById(R.id.id_of_your_listView);
The rootView is the view you get in your onCreateView Method of the Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.example_fragment, container, false);
return rootView;
}

Try using (ListView)findViewById(R.id.ListActivity) instead of getListView()

If you have the ListView in the fragment you can use:
ListView list = (ListView)findViewById(android.R.id.list);
But if you want to use getListView directly you must call the ListView's id as
android:id="#android:id/list"
Hope this help.

also note that you dont pass your ListView to the adapter but you pass the adapter to the ListView
ListView list = (ListView)findViewById(R.id.ListActivity);
list.setAdapter(adapter);

You can't extend ListActivity because 1) You are wanting to use Fragments and 2) there isn't a method setSupportActionBar for the ListActivity class...
Anyways... in your question
My activity_fragment.xml
<!-- XML code -->
You can't use findViewById to get that ListView within the Activity class because your ListView isn't within activity_main.xml.
You have to do it from within the Fragment.
public class YourFragment extends Fragment {
private ListView yourListView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_fragment, container, false);
this.yourListView = (ListView) v.findViewById(R.id.listView);
// adapter stuff...
return v;
}
}

Related

using "this" in a fragment gives me an error

public class Home extends Fragment {
ArrayList<String> notes = new ArrayList<String>();
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ListView listView = (ListView) getActivity().findViewById(R.id.listView);
notes.add("Example note");
ArrayAdapter arrayAdapter =new ArrayAdapter(this, android.R.layout.simple_list_item_1,notes);
listView.setAdapter(arrayAdapter);
return inflater.inflate(R.layout.home,container,false);
}
when i use this it gives me an error, tried using getactivity or getcontext and both crash the app when i do it
where did i go wrong
any suggestions are welcomed and thanks in advance <3
You must get corresponding fragment view by below 2 lines
View myView = inflater.inflate(R.layout.fragment_view, container, false);
ListView lv = myView.findViewById(R.id.listView);
getActivity() may be null while your fragment is in process of preparation.
Just inflate your fragment layout in onCreateView and place ListView logic to onViewCreated() or onActivityCreated
Get listview from fragment by inflate, not from parent activity
ArrayAdapter needs a context as the first parameter, so either use this.getContext() or this.getActivity() instead of passing this, which is a fragment.
Furthermore, you should move the code into onViewCreated method, as the view must be inflated before you access its elements.
And you have to use view.findViewById() instead of getActivity().
For some reason Fragments do not inherit the context from an Activity. Try this:
ListView listView = ((ListView) getActivity()).findViewById(R.id.listView);
Same works for getContext.
Context context = ((ListView) getActivity)).getApplicationContext();

How change a element of fragment.xml from MainActivity.java

I want change an element of a fragment from MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);// => this is the error
...
list.setOnItemClickListener(new Adapter.OnItemClickListener() {//this can't work because list isn't in layout=>activity_main
....
I need manipulate 'lis't from MainActivity, but i can't, because the element "list" is in the fragment container.xml and isn't in activity_main.xml .
I can't move 'list' in activity_main.xml, because i need load into container.xml.
How can i call from MainActivity in the right way?
Why you need to perform action on fragments view in activity?
You can do all this inside your fragment.
Add that fragment to activity.
In fragments onCreateView method get the view and do whatever you want.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflatedView = inflater.inflate(R.layout.your_layout, container, false);
ListView list = (ListView) inflatedView.findViewById(R.id.list);// => this is the error
list.setOnItemClickListener(new Adapter.OnItemClickListener() {});
return inflatedView;
}
Although it is a bad idea and it is better that you find a better solution for your requirement, the answer is that you can make your ListView inside fragment public static, and then perform your action on it like:
YourFragment.list.setOnItemClickListener(new Adapter.OnItemClickListener()...

Unable to add window -- token null is not for an application from ListFragment

I have this ListActivity that is populated by json data from web server, but suddenly I want to try to use ViewPager on my app.
It seems like ListActivity and ListFragment has almost the same function. So what I did was to copy-paste the codes to ListFragment, I've change a little bit of code to get rid of errors though.
What I did was to put all the codes from onCreate(ListActivity) to onCreateView(ListFragment) like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
examinationList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
new GetContacts().execute();
return rootView;
}
That created an Content view not yet created exception. So what I did was create an onViewCreated method and remove all the unnecessary codes from onCreateView and transfer it to onViewCreated
#Override
public void onViewCreated (View view, Bundle savedInstanceState) {
examinationList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
new GetContacts().execute();
}
that removed the Content view not yet created exception but now another exception appeared and I can't solve it.
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
EDITED
fragment_movies.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Solve it! The problem was on the showing of the ProgressDialog on onPreExecute, it seems like getting the context on normal activity is different from getting the context on fragments.

onListItemClick not called in ListFragment

I have a ListFragment which contains a list however onListItemClick is never called. I am not using getListView() which I suspect is the problem. I am pulling my list view from the xml as such:
list = (ListView) getActivity().findViewById(android.R.id.list);
And then setting the adapter like this:
list.setAdapter(new CustomAdapter(getActivity(), R.layout.title, mCursor, new String[]{"title"}, new int[]{R.id.my_title}) );
Since I need to set the adapter on the list, I do not use setListAdapter() either. Is it not possible to pull the list from the xml and use onListItemClick? I would like to keep my list view in the xml so I do not have to set all of the properties programmatically.
If this is not possible, how can I select items in my list?
Thanks
OK. Try this answer. Keep your layout file as it is. Override the following method in your FragmentList class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.your_fragment_layout, container, false);
}
Make sure that your layout define a ListView with the right id, as follows:
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
Then, just call FragmentList::setListAdapter:
setListAdapter(new CustomAdapter(getActivity(), R.layout.title, mCursor, new String[]{"title"}, new int[]{R.id.my_title}) );
Should work, ticky-boo.

Android ListView in Fragment

I am trying to create a list of items which contains an image and some description for the image in each individual. After which, the list will be place in a fragment inside the application. Can anyone guide me to create it? I am not too sure how can I do it without the ListActivity.
It seems like your Fragment should subclass ListFragment (not ListActivity). onCreateView() from ListFragment will return a ListView you can then populate.
Here's more information on populating lists from the developer guide: Hello, ListView
Here's a code snippet to help you display ListView in a fragment.:
public class MessagesFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_messages, container,
false);
String[] values = new String[] { "Message1", "Message2", "Message3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
return rootView;
}
}
And, this is how the xml would look like:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
If you do not use 'list' as ID or do not include a ListView into your layout, the application crashes once you try to display the activity or the fragment.
Reference: http://www.vogella.com/tutorials/AndroidListView/article.html#listfragments

Categories

Resources