There is such a class in which the custom ListView described. I need to make it so that by clicking on each item in the list, a separate DialogFragment . (First.class = DialogFragment)

This method crashes the application when you click on any item from the list:

 @Override public void onListItemClick(ListView l, View v, int position, long id){ switch(position){ case 0: i = new Intent(getActivity().getApplicationContext(), First.class); break; } startActivity(i); } 

Full class code:

  package ua.nikoz47.mobilewaiter.Tabs; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import ua.nikoz47.mobilewaiter.Food.First; import ua.nikoz47.mobilewaiter.R; public class Menu extends ListFragment{ Intent i; String[] menu_list={ "Перші страви", "Другі страви", "Десерти", "Алкогольні напої", "Безалкогольні напої"}; int[] menu_icons={ R.drawable.first, R.drawable.second, R.drawable.dessert, R.drawable.alcohol, R.drawable.soft_drinks}; ArrayList<HashMap<String, String>> data=new ArrayList<>(); SimpleAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { HashMap<String, String> map=new HashMap<>(); for(int i=0;i<menu_list.length;i++) { map=new HashMap<>(); map.put("MenuItem", menu_list[i]); map.put("MenuImage", Integer.toString(menu_icons[i])); data.add(map); } String[] from={"MenuItem","MenuImage"}; int[] to={R.id.menu_textView,R.id.menu_imageView}; adapter=new SimpleAdapter(getActivity(), data, R.layout.custom_menu, from, to); setListAdapter(adapter); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onListItemClick(ListView l, View v, int position, long id){ switch(position){ case 0: i = new Intent(getActivity().getApplicationContext(), First.class); break; } startActivity(i); } } 

Code First.class (Dialog Fragment)

 package ua.nikoz47.mobilewaiter.Food; import android.app.DialogFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SearchView; import ua.nikoz47.mobilewaiter.R; public class First extends DialogFragment { ListView lv; SearchView sv; ArrayAdapter<String> adapter; String[] first_list={ "Борщ", "Борщ Зелений", "Грибний суп", "Солянка"}; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.dialog_fragment_first, null); getDialog().setTitle("Перші страви"); lv=(ListView) rootView.findViewById(R.id.listView); sv=(SearchView) rootView.findViewById(R.id.searchView); adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, first_list); lv.setAdapter(adapter); sv.setQueryHint("Пошук страви..."); sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String txt) { return false; } @Override public boolean onQueryTextChange(String txt) { adapter.getFilter().filter(txt); return false; } }); return rootView; } } 
  • By clicking you call activates, but not DialogFragment , and in the question you ask about it. The question is how to activate the dialog instead of activating? - pavlofff
  • I apologize for the incorrect wording, I am new to Android development, the question is how to get the dialogue on click, right, First.class in my code is Dialog Fragment, I used getActivity () but to output the dialogue I need another command? - Nikoz47
  • one
    The error is that you call Fragment, like Activit. Fragments are called differently, via the FragmentManager . The getActivity() method returns the link to the activity to which the Fragment is attached (in this case it is used as a context) - it displays no dialogs and nothing at all. - pavlofff
  • Understood, apparently you need to read the philosophy of java ... can you please write a response code, how to replace my code here: public void onListItemClick (ListView l, View v, int position, long id) {I will be very grateful for the dialogue you need to call! ! I do not know how to do it ... - Nikoz47
  • one
    Although, of course, you need to read the philosophy of Java, in this case it will not help you, since these are the classes and methods of the Android framework, and not Java / I can advise B.Hardy to read "Android. Programming for professionals" 2015. How to call a fragment is written, including here, a thousand times and even here there is a search, and the resource is specifically so that in case of problems you should not wait for an answer, but get it right away by finding the right answer with a solution, but you can wait for the indefinite time, of course, when someone answers, if you do not hurry anywhere. I am inconvenient to write code from my phone. - pavlofff

2 answers 2

UPD to display a DialogFragment (your First.class ) use:

 First dFragment = new First(); dFragment.show(fragmentManager, "tag")); 

To launch an Activity :

  i = new Intent(getActivity().getApplicationContext(), First.class); 

Change to

  i = new Intent(getActivity(), First.class); 

PS Besides, you will not interfere with checking

 if(i != null) { startActivity(i); } 

and this is not a new DialogFragment , but an Activity

PPS Well, when you ask a similar question, do not forget to add the error rate frame.

  • I apologize for the incorrect wording of the question, corrected it, First.class = DialogFragment in my code, added this code in the first post, log: yadi.sk/i/-OUQ4kVgraJdE in the code I used getActivity (), but I need something else for the call, not activating, but dialogue, did I understand correctly? - Nikoz47
  • @ Nikoz47_w3bsit3-dns.com updated answer - VAndrJ

Thanks for the great help! Thanks to the post above, did:

 @Override public void onListItemClick(ListView l, View v, int position, long id){ switch(position){ case 0: p.show(getActivity().getFragmentManager(), First.class.getName()); break; } } } 

deleted:

 startActivity(i); 

It all worked

  • Instead of the resource-intensive call to First.class.getName() you can simply specify any string value (for example, "first") that will be the tag (unique identifier) ​​for your fragment in the FragmentManager - through it you can access this fragment through the manager. It will be much easier for the system. - pavlofff
  • Thank you very much, useful information for me. - Nikoz47