I have 3 classes, as follow:
Drawer.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action_center);
CustomAdapter adapter = new CustomAdapter(this, savedInstanceState);
mDrawerList.setAdapter(adapter);
}
Where the drawer is implemented. I have read that I should implement
adapter.notifyDataSetChanged();
Inside of
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
mMenuAdapter.notifyDataSetChanged();
menu.findItem(R.id.action_websearch).setVisible(true);
return super.onPrepareOptionsMenu(menu);
}
CustomAdapter.java:
Where the custom adapter is implemented.
PlanetFragment.java:
public class PlanetFragment extends Fragment {
ListView lv;
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
//#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
lv = (ListView) rootView.findViewById(R.id.list);
lv.setAdapter(SecondCustomAdapter...);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/***I want to refresh the drawer when an item on the fragment is
clicked, but I just can't figure out a way to do it...***/
}
}
}
Can you help me here please?
Thank you!
Related
Here is my code which is not working
I'm getting data from server using `AsyncTask` and set data to the `listview` using `baseadapter` but using `convertview.setonclicklistner` working in adapter class and below code contains in `Fragmentclass(import android.support.v4.app.Fragment;)` so I think it is not problematic
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_restaurants, container, false);
restaurant_list = (ListView)view.findViewById(R.id.restatrant_lv);
no_restaturants=(TextView)view.findViewById(R.id.no_restaturants);
// listening to single list item on click
restaurant_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.e("temp","temppppp");
}
});
//set layout slide listener
rowItems = new ArrayList<>();
history_list = new ArrayList<HashMap<String, String>>();
list_adapter = new ViewAdapter(getActivity(),rowItems);
restaurant_list.setAdapter(list_adapter);
}
What is the solution for this ?
add id to your parent layout for custom_list_item.xml
holder.parentLayout.setOnClickListener(new OnClickItem(position));
private class OnClickItem implements View.OnClickListener{
private int mPostion;
public OnClickItem(int position){
mPostion = position;
}
#Override
public void onClick(View v) {
YourActivity yourActivity = (YourActivity) mContext;
yourActivity.onItemClicked(mPostion);
}
}
android:descendantFocusability="blocksDescendants"
If any ListView contains focusable or clickable view then OnItemClickListener won't work. In that case we need to use this.
Try to set your setOnClickListener after setting the adapter:
Like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_restaurants, container, false);
restaurant_list = (ListView)view.findViewById(R.id.restatrant_lv);
no_restaturants=(TextView)view.findViewById(R.id.no_restaturants);
//set layout slide listener
rowItems = new ArrayList<>();
history_list = new ArrayList<HashMap<String, String>>();
list_adapter = new ViewAdapter(getActivity(),rowItems);
restaurant_list.setAdapter(list_adapter);
// listening to single list item on click
restaurant_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.e("temp","temppppp");
}
});
}
I have an activity which calls several fragments. In one of those fragments I am trying to create a dialog spinner programmatically and add it to the menu option (in toolbar). I manage to make it work (atleast the view is showing), but the onItemSelected it is not getting called.
My code:
public class NewsFeed extends Fragment {
private static final String TAG = "tag";
private String tag;
private ArrayAdapter<New> newsadapter;
private ArrayAdapter<Tag> tagsadapter;
private Spinner spinner;
public NewsFeed() {
setHasOptionsMenu(true);
}
public static NewsFeed newInstance(String tag) {
NewsFeed fragment = new NewsFeed();
Bundle args = new Bundle();
args.putString(TAG, tag);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
tag = getArguments().getString(TAG);
}
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setTitle(tag);
NavigationView activitynav = (NavigationView) getActivity().findViewById(R.id.nav_view);
BottomNavigationView activitybuttomnav = (BottomNavigationView) getActivity().findViewById(R.id.navigation);
activitynav.setCheckedItem(R.id.nav_news);
activitybuttomnav.getMenu().getItem(1).setChecked(true);
View v = inflater.inflate(R.layout.fragment_news_feed, container, false);
//Spinner related code
tagsadapter = new TagsAdapter(getActivity(), android.R.layout.simple_spinner_item, (ArrayList<Tag>) ApplicationData.tags);
spinner = new Spinner(getActivity(), Spinner.MODE_DIALOG);
tagsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setPrompt("Filtrar por categoria:");
spinner.setAdapter(tagsadapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
newsadapter = new NewsAdapter(getActivity().getApplicationContext(), 0, (ArrayList<New>) ApplicationData.news);
ListView listview = (ListView) v.findViewById(R.id.listview);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
NewsDisplay nd = NewsDisplay.newInstance(tag, ApplicationData.news.get(position).getNewsUrl());
ft.addToBackStack(null);
ft.replace(R.id.fragmentcontent, nd).commit();
}
});
listview.setAdapter(newsadapter);
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.filter_category) {
//For showing the spinner
spinner.performClick();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Already tried:
To make NewsFeed implementing OnItemSelectedListener.
To create the OnItemSelectedListener object inside SetOnItemSelectedListener.
Could you give me some help? It might have to do with this being a fragment and the toolbar is in the activity.
Spinner that was created dynamically (private val popupSpinner: Spinner by lazy { Spinner(this, Spinner.MODE_DIALOG) }), needs to have an assigned parent view. If it does not have it, the OnItemSelected is never called for some reason.
I wrote this hack to alleviate the problem. Added to parent view and hid it. Be sure not to run this in onResume() method and similar ones.
findViewById<ViewGroup>(android.R.id.content).addView(popupSpinner)
popupSpinner.visibility = View.INVISIBLE
PS. Some people might think that this question does not have an answer. But in the comments the author himself found a solution.
I'm building a very simple NBA App since I'm new to this.
The MainActivity has a ListView with two items. Western teams and Eastern Teams. When the user taps on an item, it shows the teams of that conference. These teams are stored as array items in the strings.xml file.
Here is the MainActivity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
ArrayAdapter<String> mConferenceAdapter;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] conferenceArray = {
"Western Conference",
"Eastern Conference"
};
List<String> conferenceList = new ArrayList<String>(Arrays.asList(conferenceArray));
mConferenceAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_conferences, // The name of the layout ID.
R.id.list_item_conference_textview, // The ID of the textview to populate.
conferenceList);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.listview_conferences);
listView.setAdapter(mConferenceAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String forecast = mConferenceAdapter.getItem(position);
Intent intent = new Intent(getActivity(), WesternConference.class)
.putExtra(Intent.EXTRA_TEXT, forecast);
startActivity(intent);
}
});
return rootView;
}
}
The issue I have now is that no matter which selection I choose it only shows the Western teams.
This is a snippet from WestConference.java:
public static class PlaceholderFragment extends Fragment {
ArrayAdapter<String> mConferenceAdapter;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create some dummy data for the ListView. Here's a sample weekly forecast
String[] teams = getResources().getStringArray(R.array.westTeams);
List<String> conferenceList = new ArrayList<String>(Arrays.asList(teams));
// Now that we have some dummy forecast data, create an ArrayAdapter.
// The ArrayAdapter will take data from a source (like our dummy forecast) and
// use it to populate the ListView it's attached to.
mConferenceAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_conferences, // The name of the layout ID.
R.id.list_item_conference_textview, // The ID of the textview to populate.
conferenceList);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.listview_conferences);
listView.setAdapter(mConferenceAdapter);
return rootView;
}
}
How can I make it to where if I click West, it loads those teams, and if I click East, it loads the eastern teams?
I'm using fragments and I have a menu item in one of my fragments. The fragment that uses the menu item is called when an item in the List View is clicked. When the fragment with the menu item is called, and then I change the tab, the menu item is still there. I want the menu item to only show in the one fragment only.
Here is the code for Tab 1
public class Tab2Fragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_fragment, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().setTitle("Tab 2");
initView();
}
private void initView() {
((TextView)getView().findViewById(R.id.tab_text)).setText("Tab 2");
Button button = (Button) getView().findViewById(R.id.tab_btn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment();
}
});
}
private void replaceFragment() {
((BaseContainerFragment)getParentFragment()).replaceFragment(new Tab2AddOnFragment(), true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
and Here is the code for Tab 3 which has the List View
public class Tab3Fragment extends ListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Ang na Ang na", "Sulakihin", "Somaskidot", "Moko Moko"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
getActivity().setTitle("Tab 3");
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
switch(position){
case 0:
// Create new fragment and transaction
FragmentTransaction transaction = getFragmentManager().beginTransaction();
replaceFragment();
transaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// Commit the transaction
transaction.commit();
break;
default:
}
}
private void replaceFragment() {
((BaseContainerFragment)getParentFragment()).replaceFragment(new FragmentInListView(), true);
}
}
and here is the code for the fragment with the menu item
public class FragmentInListView extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getActivity().setTitle("Fragment Called in Tab 3(List View)");
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment,
container, false);
TextView tv= (TextView) view.findViewById(R.id.tab_text);
tv.setText("Fragment in List View");
return view;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.travel_tips_expanded, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
and here is the code for the menu of the fragment with a menu item
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/favorite"
android:showAsAction="always"
android:title="favorite"/>
Here is an image of the Fragment called when an item in the List View is clicked. And it contains the menu item.
http://s904.photobucket.com/user/fookywooky/media/tab4_zps30944a6e.png.html
And here is an image of the Fragment called when "Tab 2" is selected. As you can see, the menu item is still there but i don't want it to be there. Also, this is the case even if you click Tab 1 and Tab 3. The menu item is still there.
http://s904.photobucket.com/user/fookywooky/media/tab5_zps285c8314.png.html
Please help. Thanks in advance.
In order to accurately determine when to show the Fragment menu, your Activity needs some indication as to which Fragment is visible and current. You can accomplish this with the setMenuVisibility() and setUserVisibleHint() methods. In your OnTabChanged() callback, set the menu visibility and user visible hint to false in the previously selected Fragment and true in the currently selected Fragment.
My onListItemClick is never call when i click on item, the class is extends fragment not listfragment, because i have other view items in this fragment which is not list, so how to implement onlistitemclick in class extends fragment?
class
public class MainFiles extends Fragment
{
ArrayList<String> items;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.files, container, false);
Button button_up = (Button) view.findViewById(R.id.button_up);
items = new ArrayList<String>();
MyAdapter adapter = new MyAdapter(getActivity(), R.layout.row, items);
ListView myList = (ListView) view.findViewById(R.id.list);
myList.setAdapter(adapter);
return view;
}
public void onListItemClick(ListView l, View v, int position, long id)
{
}
}
Explicitly add the OnItemClickListener to your ListView
myList.setOnItemClickListener(this);
You must also make sure that your Fragment implements the OnItemClickListener type:
public class MainFiles extends Fragment implements OnItemClickListener
Another way is to create a dedicated subclass of OnItemClickListener to pass to the ListView:
myList.setOnItemClickListener(new MyOnItemClickListener());
/* ... */
private class MyOnItemClickListener implements OnItemClickListener {
/* ... */
}
You forget to set the setOnItemClickListener
after myList.setAdapter(adapter); add this:
myList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position,long id){
new File(items.get(position));
fileList(path.get(position));
showPath(current_path);
}
});
I have implement listener in fragment like this
public class DetailsFragment extends Fragment implements OnItemClickListener {
private ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_details, container, false);
// list view
listView = (ListView) root.findViewById(R.id.listView);
listView.setListAdapter(new DetailsAdapter(getActivity(), list));
listView.setOnItemClickListener(this);
return root;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// handle click here
}
}
Your fragment has to extend ListFragment, you can have more things in your layout other than a list if your layout has a listview with id=android:id/list