0

I need help in order to achieve my search function on action bar. IThis tutorial shown how to do list view using simple list item, while my code, I used custom Adapter class.

Let
  • 33
  • 1
  • 6

3 Answers3

1

instead of this code on getVeiw method :

view = getLayoutInflater().inflate(R.layout.list_black_text, null);

use this one

 if (view == null) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        view= inflater.inflate(R.layout.list_black_text, null);
    }

maybe this work for you

e.hadid
  • 999
  • 9
  • 20
  • context.getSystemService and convertView give me error – Let Apr 16 '17 at 07:41
  • because you are in mainActivity you don't need context see it again i update that – e.hadid Apr 16 '17 at 07:45
  • i fixed it for you lock at the answer again. – e.hadid Apr 16 '17 at 07:51
  • no more error but my apps still stop when I run its. When I click debug its show me this line error : searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() – Let Apr 16 '17 at 08:02
0

Looks like your searchView is null. Instead of this:

inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.item_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

Use this:

inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.item_search);
SearchView searchView = (SearchView) searchItem.getActionView();

And take a look to documentation.

EDIT

I copy your code and I tested. I found an error. I don't know if is another error or is the same error that you are having.

When you declare ListView variable, you are setting to global scope but when you give it a value (R.id.listView value) you are declaring another ListView variable with method scope.

Don't do this ListView listView = (ListView) findViewById(R.id.listView);

Do this listView = (ListView) findViewById(R.id.listView);

Making that change, this line: listView.setAdapter(customAdapter); will not give you NullPointerException.

I hope that fix your code. In other case, you should put your logcat output in your question for further details. And in your future questions too ;).

jorgeavilae
  • 208
  • 1
  • 9
  • I already changed but my apps still stop. Its still give me error on ths line searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){ – Let Apr 16 '17 at 08:46
  • And how looks your menu.xml file? – jorgeavilae Apr 16 '17 at 08:47
  • Hi jorge. I already try. When i change custom adapter to adapter its give error. – Let Apr 18 '17 at 10:54
  • @Let you dont need to change adapter, just change listView line – jorgeavilae Apr 18 '17 at 10:57
  • no more error @jorge bu when i run the apps crash and give me this line : – Let Apr 18 '17 at 11:03
  • searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){ – Let Apr 18 '17 at 11:04
  • java.lang.NullPointerException at com.example.myuser.customlistview.MainActivity.onCreateOptionsMenu(MainActivity.java:88) – Let Apr 18 '17 at 11:04
  • In menu.xml you have `app:actionViewClass="android:.support.v7.widget.Searchview"`. Delete two dots ":" and Serchview to SearchView `app:actionViewClass="android.support.v7.widget.SearchView"` – jorgeavilae Apr 18 '17 at 13:05
  • You have to make that item from menu.xml resources be the same class that you are importing in your MainActivity.java `import android.support.v7.widget.SearchView` – jorgeavilae Apr 18 '17 at 13:08
  • You have to make that item from menu.xml resources be the same class that you are importing in your MainActivity.java import android.support.v7.widget.SearchView --- > what do you mean by this? – Let Apr 18 '17 at 13:29
  • The class in app:actionViewClass must be the same class that you import and use in the MainActivity, the SearchView class. Thats why searchView object, wich is a instance of SearchView class, is giving you NullPointerException, because there isn't item in menu.xml with that class since you are writing wrong. – jorgeavilae Apr 18 '17 at 13:34
  • the apps and search button appeared, when I try to search the apps stop. – Let Apr 18 '17 at 13:45
  • So the problem with the SearchView is solve!! Dont forget to mark this answer as correct ;) Your new problem is because of the ArrayAdapter instantiation. You should write `new ArrayAdapter<> (MainActivity.this, R.layout.list_black_text, R.id.textView2, templist);` You can easily find more info in [google](https://www.google.es/search?q=ArrayAdapter+requires+the+resource+id+to+be+a+textview&oq=ArrayAdapter+requires+the+resource+id+to+be+a+textview) or [here](http://stackoverflow.com/questions/23020408/java-lang-illegalstateexception-arrayadapter-requires-the-resource-id-to-be-a-t) – jorgeavilae Apr 18 '17 at 14:14
  • Its working no more error. But actually i want it search both two lines of text.Now its only show the uppercase part. the lowecase part only appear as textview when we search – Let Apr 18 '17 at 14:25
  • When i do searching its only textVIew2 appear but not textView3. Bcs textView2 and 3 is the same things. only small and capital letter. When i close search button then the textView3 no longer display. – Let Apr 18 '17 at 14:30
  • Thats because in onCreate you set the adapter for the listview as a customadapter, but in searchview.onquerytextlistener you set the adapter for the listview as a normal arrayadapter. – jorgeavilae Apr 18 '17 at 14:39
  • how can i set in searchview.onquerytextlistener as customadapter? because i need smth like tht.so it can both appear at textView2 and 3 when I run its ;) – Let Apr 18 '17 at 14:41
  • https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView – jorgeavilae Apr 18 '17 at 14:48
  • Do you mean i need to define class for textview 2 and textview 3 on main activity? and do i need to create new xml file for view template or just using list_black_item? – Let Apr 18 '17 at 14:50
  • You should use your custom adapter to assign values to list_black_item.xml in the same way the tutorial use UserAdapter to assing values to item_user.xml – jorgeavilae Apr 18 '17 at 15:01
  • These are very basic questions and you should ask in another post. I solve your question for searchview and the problem was you are not typing exactly what is in the youtube video. Now you have another problem implementing a custom adapter for your listview and i give you a good tutorial. Just follow that tutorial and learn from it and if you dont understand something ask in another post – jorgeavilae Apr 18 '17 at 15:11
0

This line of code give you null pointer exception because it does not exist in your current layout. Check your xml File man!

searchView.setOnQueryTextListener(new  SearchView.OnQueryTextListener(){

And look through your code there is a missing ; after search view declaration

SearchView searchView = (SearchView)