I'm trying to update the subtitle of my toolbar from an adapter class. I've tried the accepted answer from here.
((OrderActivity) context.getSupportActionBar().setSubtitle("Bla Bla"));
Order Activity is the calling activity and this line of code is in the adapter. The context being used here is passed from the calling/parent activity.
I'm getting a 'cannot resolve method getSupportActionBar()'.
try ((OrderActivity) context).getSupportActionBar().setSubtitle("Bla Bla");
Related
I have an activity called PeopleActivity, with a layout xml of activity_people.xml.
This activity is using the Android Studio Navigation Drawer Activity template.
I have a list view in the content_people.xml file named people_list. When I attempt to associate this control in the back end, it is unable to find the id. I am attempting it as:
peopleList = (ListView) findViewById(R.id.people_list);
Any insight would be greatly appreciated.
EDIT: Did an update to the code section to reflect the correct control referenced.
you should declare your listview like this:
peopleSearch = (ListView) findViewById(R.id.people_list);
not as EditText
I found the answer. You cannot use capital letters in the naming of your controls. Once I fixed this, everything worked fine.
I have a List of fragments which i can open in my navbar with:
getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, l_Fragment).commit();
The fragments are all from the same type -> CDMBasicMovieFragment extends Fragment
Inside this fragment i have a viewpager which i call with getFragmentManager(). And here i have the problem. The first fragment is fine but after replacing the first fragment, the viewpager is empty. Then i changed getFragmentManager() to getChildFragmentManager(). Now all fragments are fine. But only 1 time. After replacing back to the first fragment i have the same problem. What am i doing wrong?
Edit1:
My view hierarchy:
Activity
CDMBasicMovieFragment 1
ViewPager
CDMMovieListFragment
CDMBasicMovieFragment 2
ViewPager
CDMMovieListFragment
CDMBasicMovieFragment 3
ViewPager
CDMMovieListFragment
Edit2:
I tried to create unique ids for the fragments and used them on replace but i already have the same problem:
getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, l_Fragment, uniqueFragmentTag).commit();
Edit3:
I found a possible solution:
OnCreateView called multiple times / Working with ActionBar and Fragments
I tried everything from this link. I already have the same problem... Can anyone help?
UPDATE:
After a intensive debug session ;) i found this:
When loading the first CDMMovieListFragment FragmentPagerAdapter's instantiateItem is called and checks if there's already a fragment. It's not thats why it internally calls
mCurTransaction.add(container.getId(), fragment, makeFragmentName(container.getId(), itemId));
When i replace the CDMBasicMovieFragment which includes the fragmentpageradapter (inside is the added CDMMovieListFragment) and try to load the second CDMBasicMovieFragment then it's still the same fragmentmanager.
FragmentPagerAdapter's instantiateItem is called and found the old CDMMovieListFragment. Now it calls mCurTransaction.attach(fragment); instead. I think here's the problem. But im so confused...
I resolved my problem.
Now i always replace the CDMBasicMovieFragment like this:
getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, CDMBasicMovieFragment.newInstance()).commit();
On calling the FragmentPagerAdapter's constructor i changed getFragmentManager to getChildFragmentManager.
I thought i can create all fragment one time and just replace them if i click on the item of the navigationbar (drawner). But the replace will remove the fragment which was created one time and when i want to replace the removed fragment again, it's not there. That's why i got an empty viewpager.
Try below code snippet, In your custom pager adapter:
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Then in your Activity containing the ViewPager:
final MyPagerAdapter adapter = new MyPagerAdapter();
pager.setAdapter(adapter);
pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
... anything you may need to do to handle pager state ...
adapter.notifyDataSetChanged(); //this line will force all pages to be loaded fresh when changing between fragments
}
}
Can you help me about this error on my logcat?
http://pastebin.com/uSXruD54
Where:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference
on my Home class at line 79 which is:
drawer.setDrawerListener(toggle);
http://pastebin.com/cixZ7d9d (MainActivity class, line 48)
I don't have any idea how setDrawerListener works sorry, I found the same problem here on stackoverflow: How to rectify NullPointerException in v4.DrawerLayout?
The answer says that it has to make sure that I'm using the same id for nav drawer and in layout file but I didn't made any changes on nav drawer because it's the activity itself that I chose in Android Studio, I just implemented tabs on it.
drawer is null, presumably because the DrawerLayout with ID drawer_layout is not in the app_bar_home layout. You should only be calling setContentView() once in onCreate(). The second call is completely replacing the activity_home layout with the app_bar_home layout. Your DrawerLayout is then no longer in the Activity, and the findViewById() will return null.
Instead, <include> the app_bar_home layout in activity_home, and remove the second setContentView() call.
I have a ListView that gets filled by SimpleAdapter, this adapter is local variable inside the procedure that fills the ListView.
Now, I want to filter the ListView, but I have no access to the adapter in the EditText events
is there a way to do that?
I tried MyListView.getAdapter().getFilter().Filter("some text");
but that gives error as getFilter does not exist for MyListView.getAdapter()
How can I fix this?
Declare adapter as global, setting adapter as global will let you filter it:
YourActivity.this.adapter.getFilter().filter("some text");
Did you try this?, it should works:
Adapter adapter= MyListView.getAdapter();
adapter.getFilter().filter("some text");
Too you can post your code to check what are you doing wrong, if you're using a custom adapter you have to implement the getFilter method
This post might answer your question. You have to create getFilter() in your adapter.
I am new to Android Development and I have a simple list app which I have been asked to create.I have had no problems having the app as activity based however I have to extend the functionality and use fragments for a 'universal' app. My main activity is:
I was able to successfully compile your code by taking the following steps:
It looks like this line is the problem (inside Main.java):
contactCursor = contactDBAdapter.getAllContactsCursor();
I looked at how your contactDBAdapter gets initialized and it turns out you initialize it after you setContentView for your activity. However, your view involves calls to contactDBAdapter. So in Main.java you need to move the following two lines to the TOP of the onCreate window:
public void onCreate(Bundle savedInstanceState)
{
contactDBAdapter = new ContactDBAdapter(this);
contactDBAdapter.open();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
....
}
Furthermore, the following line in Main.java needs to be removed (or commented out):
contact.clear();
Also, I had to make two further changes to how you call ListView
In list_view.xml, the way you identify a ListView for Android is :
android:id="#+id/android:list"
In ContactListFragment.java, then call the ListView this way :
parent.myListView = (ListView)v.findViewById(android.R.id.list);
have you not just tried using the Eclipse Template which set up everything for you just copy in your existing code?
File>New>Android Application Project then under the Create Activity Step select
Your Fragment class needs an empty default constructor. See Android Reference