v7 searchview overlapping issue - java

example to what kind of problem I am running into.
Let say I have 6 elements in a listview all in order from left to right [1,2,3,51,4,5].
When I search for 5 it displays "51" and "5". When I click on "51" it opens up "1". The problem is that the filtered list overlaps the listview that displays all the elements. I was wondering how I can fix this issue. I am using v7.widget.SearchView
#Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.recipe_menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.search_recipe).getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
lv.clearTextFilter();
} else {
lv.setFilterText(newText.toString());
}
return true;
}
});
super.onCreateOptionsMenu(menu, inflater);
}

I think that you make mistake in onItemClick(AdapterView<?> parent, View view, int position, long id) method. You take data by position from data list. The "position" is a position of clicked item on list. So if you filtered your list and click on first item the value of position will be "1". You have to take value from adapter which is corresponding to clicked position. You should take item from adapter like is shown in code below:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
String data = ((ExerciseDAO)parent.getAdapter().getItem(position)).getTitle() ;
...
}
});
If you have questions - just ask.

Related

Android: Tabs and searchbars

I have two tabs and each tabs has its own searchbar.
I bind the searchbar in onCreateOptionsMenu. However, the searchbars only work if I leave the screen once and return to the screen (meaning it needs one more lifecycle for the searchbars to react). I confirmed that onCreateOptionsMenu is indeed called two times at the time of the creation of the ViewPagerFragment.
I bind them like this:
MenuItem searchItem = menu.findItem(R.id.search);
searchItem.setVisible(true);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
...
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
...
return false;
}
});
I am guessing this bug is related to the tabs. How do implement a working searchbar with tabs (i.e. viewpager2)?
I call this on onCreateOptionsMenu:
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
// Call the above...
}
The ViewPager hosting them looks like this:
private void init(View view) {
ViewPager2 viewPager2 = view.findViewById(R.id.view_pager_fragment_view_pager);
TabLayout tabLayout = view.findViewById(R.id.tab_layout_fragment_view_pager);
viewPager2.setUserInputEnabled(true);
viewPager2.setAdapter(new ViewPagerFragmentAdapter(ViewPagerFragment.this));
viewPager2.setOffscreenPageLimit(5);
new TabLayoutMediator
(tabLayout, viewPager2,
(tab, position) -> tab.setText(titles[position])).attach();
}
OP and I were communicating while we found a solution.
First each fragment was changed to
public class MergedItemsFragment extends Fragment implements SearchView.OnQueryTextListener {
/// ------------------------------
/// SearchView.OnQueryTextListener
#Override
public boolean onQueryTextSubmit(String query) {
// ...
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// ...
return false;
}
/// --------
/// Fragment
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_merged_items, container, false);
}
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
final MenuItem searchItem = menu.findItem(R.id.search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
searchView.setOnQueryTextListener(this);
}
}
SearchView was inflated by using a menu.xml file within the App's Activity.
SearchView.OnQueryTextListener was implemented by each Fragment that needed access to the SearchView
Since onCreateOptionsMenu() is invoked each time a Fragment is created, or subsequently comes into view (e.g. Swipe); the SearchView's OnQueryTextListener is updated to the corresponding Fragment
Lastly, there was a line in OP's main Fragment containing the ViewPager2, which was removed: viewPager2.setOffscreenPageLimit(5); that caused each Fragment provided by the FragmentStateAdapter to instantiate and mutate the SearchView's OnQueryTextListener each time a Fragment was created.
Removing the line made sure that only the Fragment that was in view was bound to the Toolbar's SearchView.
If any more code is desired, i'd be happy to post what I have, and if I come up with a solution using viewPager2.setOffscreenPageLimit(5); i.e. caching, i'll post that as well

Using a space key in searchview to filter a listview

I have succeeded to implement a searchview to filter in real time my ListView except the searchview doesn't accept to type a keywords containing a key space...
If I'm typing "Test", all my row that contain "Test" are appearing but if I type "Test number", nothing appear even if there are a few match in my ListView.
I found two posts that treat similar problems but even after I read it, I do not understand from where this issue come from.
Do you know how to solve this ?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.persmenu, menu);
MenuItem item = menu.findItem(R.id.menuSearch);
searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return true;
}
EDIT : the problem seems different than what I was thinking. The space key is taken into account but the dapter.getFilter().filter(newText) doesn't seems to filter all the textview by the same way.
It applied the query on the 1 textview of the filter. If i'm putting the content of the second textview of the adapter in the 1 textview, the query is working and I find the right information.
So, know, I need to understand why the filter applied in the adapter but only in the first textview and not in all...

Why is the wrong ListView item highlighted?

I implemented a ListView, and the method OnItemLongClick to highlight the selected item, but when I select an item, if I scroll the ListView, I find that another item is highlighted.
I'm using this code:
list.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
arg1.setBackgroundColor(Color.BLUE);
return true;
}
});
you can use android:listSelector property instead of onItemLongClick

how to highlight the selected item in the menu on the navigation bar drawer

i already created a navigation drawer bar with a menu but i want to highlight and disable the selected item on the menu bar when i check the drawer bar.
how can i do that ?
also i did not see the click animation when i click the item on the menu
please help
Thanks.
this is the code i used
mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
if(child!=null && mGestureDetector.onTouchEvent(motionEvent)){
Drawer.closeDrawers();
Toast.makeText(MainActivity.this, "The Item Clicked is: " + recyclerView.getChildPosition(child), Toast.LENGTH_SHORT).show();
int pos = recyclerView.getChildPosition(child);
if(pos== 1){
Intent intent = new Intent(getApplicationContext(),About.class);
startActivity(intent);
}
return true;
}
return false;
}
#Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
RecyclerView does not handle item selection or states like a ListView does. Instead you have to handle this manually in your view holder.
The first thing you can do is declare your item view as clickable, in your `ViewHolder constructor :
public ViewHolder(View itemView) {
super(itemView);
// Make this view clickable
itemView.setClickable(true);
// ...
}
http://www.grokkingandroid.com/statelistdrawables-for-recyclerview-selection/

Detect the GridView item selected in the method onContextItemSelected

I have a GridView with an ArrayAdapter and I'd like to detect the context item selection and show a "Delete" option for deleting the object selected. I fill the grid with imageviews correctly only need the detect the delete petition. My Code:
ArrayList<MyClass> array;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.maingrid);
array=Manager.getMyArray();
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new mArrayAdapter(this,array) );
registerForContextMenu(gridview);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, DELETE_ID, 0 , R.string.delete);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
return true;
}
return super.onContextItemSelected(item);
}
How can I guess whats the element of the array I have to remove? Thanks
In onContextItemSelected try this:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
you can get the selected item like this:
youradapter.getItem((int)info.id))
override the getItem() function in the adapter to return the selected item..

Categories