the problem in search is start only of first word
example : search in "Real Madrid" , if search of "Madrid" not give result , but if search of "Real" give me result because the word "real" is first word
So what is the solution to this problem? Thank you :)
public void setAdapterToListview() {
objAdapter = new LatestGridAdapter(getActivity(), R.layout.latest_lsv_item,
arrayOfLatestVideo,columnWidth);
lsv_latest.setAdapter(objAdapter);
}
public void showToast(String msg) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
final SearchView searchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
final MenuItem searchMenuItem = menu.findItem(R.id.search);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus) {
searchMenuItem.collapseActionView();
searchView.setQuery("", false);
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
textlength=newText.length();
arrayOfLatestVideo.clear();
for(int i=0;i< allArrayVideoName.length;i++)
{
if(textlength <= allArrayVideoName[i].length())
{
if(newText.toString().equalsIgnoreCase((String) allArrayVideoName[i].substring(0, textlength)))
{
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayVideoId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
objItem.setCategoryName(allArrayVideoCatName[i]);
objItem.setDescription(allArrayVideoDesc[i]);
objItem.setVideoId(allArrayVideo[i]);
objItem.setVideoName(allArrayVideoName[i]);
objItem.setVideoUrl(allArrayVideourl[i]);
objItem.setImageUrl(allArrayImageUrl[i]);
arrayOfLatestVideo.add(objItem);
}
}
}
setAdapterToListview();
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
});
}
Try this method for searching. If it returns anything other than -1 it means that it's found a match. I turn everything into lowercase so you don't have to worry about case sensitivity.
#Override
public boolean onQueryTextChange(String newText) {
textlength=newText.length();
arrayOfLatestVideo.clear();
for(int i=0;i< allArrayVideoName.length;i++)
{
if(textlength <= allArrayVideoName[i].length())
{
if(allArrayVideoName[i].toLowerCase().indexOf(newText.toLowerCase())!=-1)
{
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayVideoId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
objItem.setCategoryName(allArrayVideoCatName[i]);
objItem.setDescription(allArrayVideoDesc[i]);
objItem.setVideoId(allArrayVideo[i]);
objItem.setVideoName(allArrayVideoName[i]);
objItem.setVideoUrl(allArrayVideourl[i]);
objItem.setImageUrl(allArrayImageUrl[i]);
arrayOfLatestVideo.add(objItem);
}
}
}
setAdapterToListview();
return false;
}
Related
When I split the char how can use spannable or ClickableSpan to get values?
this is a code
StringBuilder sb = new StringBuilder();
String data = newFeedModel.getFeedTags();
String[] items = data.split("#");
for (String item : items)
{
if(item.trim().length() == 0){
sb.append("");
}else{
sb.append("#"+item+"\n");
}
}
holder.tags.setText(sb.toString());
I will split all "#" char, like facebook function.
#aaa#bbbccc#xcvsdfsefsefa#22
it will split like this
#aaa
#bbbccc
#xcvsdfsefsefa
#22
but how to know when I split the char then can got correct gata?
Update:
public void onBindViewHolder(ViewHolder holder, int position) {
if(!TextUtils.isEmpty(newFeedModel.getFeedTags())){
holder.tags.setVisibility(View.VISIBLE);
String data = newFeedModel.getFeedTags();
holder.bind(data);
}
}
in MyClickableSpan
class MyClickableSpan extends ClickableSpan {
String clicked;
Context context;
public MyClickableSpan(Context context, String string) {
super();
this.context = context;
clicked = string;
}
public void onClick(View tv) {
Toast.makeText(context.getApplicationContext(), ""+clicked, Toast.LENGTH_LONG).show();
}
#Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false); // set to false to remove underline
}
}
how can send the data to live search when user click the tags.
this is my live code
public void fetchSearch(String key)
{
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<SearchModel>> call = apiInterface.getSearch(key);
call.enqueue(new Callback<List<SearchModel>>() {
#Override
public void onResponse(Call<List<SearchModel>> call, retrofit2.Response<List<SearchModel>> response) {
searchModelList = response.body();
adapter = new NewSearchAdapter(getActivity(), searchModelList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<List<SearchModel>> call, Throwable t) {
}
});
}
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, #NonNull MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
fetchSearch(query);
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
fetchSearch(newText);
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.message:
Intent intent = new Intent(getContext(), NotificationMessageActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
This is my OnCreateOptionMenu
An error is showing in setOnQueryListener
here contact_object is model class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.search_list,menu);
final MenuItem myMenuItem=menu.findItem(R.id.search_contact);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem (R.id.search_contact) .getActionView ();
changedSearchViewTextColor(searchView);
// ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)).setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
if (!searchView.isIconified()){
searchView.setIconified(true);
}
myMenuItem.collapseActionView();
return false;
}
}
#Override
public boolean onQueryTextChange(String newText) {
newText=newText.toLowerCase();
final List<Contact_Object> filtermodelist=filter(contacts,newText);
adapter.setFilter(filtermodelist);
return true;
}
});
return true;
}
I have implemented the filter option as below
public List<Contact_Object> filter(List<Contact_Object> p,String query )
{
query=query.toLowerCase();
final List<Contact_Object> filterModeList=new ArrayList<>();
for (Contact_Object model :p){
final String text=model.getName().toLowerCase();
if (text.startsWith(query)){
filterModeList.add(model);
}
}
return filterModeList;
}
I have declared the setFilter function as follow in CustomAdapter class
public void setFilter(List<Contact_Object> listItem){
parentList=new ArrayList<>();
parentList.addAll(listItem);
notifyDataSetChanged();
}
I have tried hard but didn't get the appropriate answer
please anyone help me,waiting for answer
There is no logic in onQueryTextChange because newText is not passed anywhere. You are passing the empty list to the setFilter method.
#Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
final List<Contact_Object> filterModeList=new ArrayList<>();
for (Contact_Object obj : p) {
String name = obj.getName().toLowerCase();
if(name.contains(newText))
filterModeList.add(obj);
}
adapter.setFilter(filtermodelist);
return true;
}
Currently, I'm working on an activity that has a RecyclerView and SearchView in it.Now the RecyclerView and SearchView are working fine but there is a problem that I don't know how to resolve, you see in the searchView part we were a task to make a SearchView that accepts 2 or more searchItems that must be separated by a comma or space in order to filter the recyclerview again if the second searchitem matches an item in the RecyclerView, for example I type in searchitem "Big" i'll need to separate it again with another searchitem like "Black"
here is the code for the SearchView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pet_book_main);
if(savedInstanceState != null){
mSearchString=savedInstanceState.getString(SEARCH_KEY);
}
pet_title=(TextView)findViewById(R.id.pet_book_title);
petbook=(RecyclerView)findViewById(R.id.pet_book_pet);
navbot=(BottomNavigationView) findViewById(R.id.petbook_navbot);
toolbar=(Toolbar)findViewById(R.id.petbook_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setTitle("");
linearLayoutManager=new LinearLayoutManager(this);
petbook.setHasFixedSize(true);
petbook.setLayoutManager(linearLayoutManager);
refresh();
disableShiftMode(navbot);
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater menuInflater= getMenuInflater();
menuInflater.inflate(R.menu.petbook_searck,menu);
searchItem=menu.findItem(R.id.pet_book_search_mode);
searchView =(SearchView)MenuItemCompat.getActionView(searchItem);
if (mSearchString != null && !mSearchString.isEmpty()) {
searchItem.expandActionView();
searchView.setQuery(mSearchString, true);
searchView.clearFocus();
}
search(searchView);
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
pet_title.setVisibility(View.INVISIBLE);
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
pet_title.setVisibility(View.VISIBLE);
return true;
}
});
return true;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mSearchString = searchView.getQuery().toString();
outState.putString(SEARCH_KEY, mSearchString);
}
public void search(android.support.v7.widget.SearchView search)
{
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
newText=newText.toLowerCase();
newList=new ArrayList<>();
for(petbook_getItem items : bookItem){
String pets=items.getPet_name().toLowerCase();
if(pets.contains(newText))
newList.add(items);
}
adapter.filterSearch(newList);
adapter.notifyDataSetChanged();
return true;
}
});
}
This code handles user input in search which can be multiple comma separated search items:
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
.....
#Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
// remove leading comma or space
String string2 = newText.replaceAll("^[,\\s]+", "");
// return early for invalid input (empty)
if (string2.isEmpty()) {
return true;
}
// split by comma & space if any
List<String> inputList = Arrays.asList(string2.split("[,\\s]+"));
newList = new ArrayList<>();
for (petbook_getItem items : bookItem){
String pets = items.getPet_name().toLowerCase();
// Check for matching by looping through each search items
for (String inputTxt : inputList) {
if (pets.contains(inputTxt)) {
newList.add(items);
break;
}
}
}
adapter.filterSearch(newList);
adapter.notifyDataSetChanged();
return true;
}
});
I am stumbed with animated searchview onQueryTextListener. When activity and fragment created first it works nice. Then I press home button, open other apps, do some work there to wipe the data of searchview activity and then return to the app. And when activity and fragment resume onQueryTextChange method is triggered by it's own. I tried this issue
Fragment replacement triggers onQueryTextChange on searchview
but it did not help, helps only when searchview SHOW_AS_ACTION_NEVER, but in this case I can not see searchview. How to prevent self-triggering of OnQueryTextListener?
Snippet from fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
searchView = new SearchView(getSherlockActivity().getSupportActionBar()
.getThemedContext());
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
fpAdapter.getFilter().filter(newText);
} else {
loadData();
}
return false;
}
});
TextView searchText = (TextView) searchView
.findViewById(R.id.abs__search_src_text);
searchText.setTextColor(Color.WHITE);
searchText.setCursorVisible(false);
ImageView searchButton = (ImageView) searchView
.findViewById(R.id.abs__search_button);
searchButton.setImageResource(R.drawable.search_menu_button);
LinearLayout searchEditFrame = (LinearLayout) searchView
.findViewById(R.id.abs__search_edit_frame);
searchEditFrame.setBackgroundResource(android.R.color.transparent);
View searchPlate = searchView.findViewById(R.id.abs__search_plate);
searchPlate.setBackgroundColor(Color.argb(0, 0, 0, 0));
menu.add("Search")
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
final MenuItem searchMenuItem = menu.getItem(0);
final Animation in = AnimationUtils.loadAnimation(getSherlockActivity()
.getApplicationContext(), R.anim.search_in);
final Animation out = AnimationUtils.loadAnimation(
getSherlockActivity().getApplicationContext(),
R.anim.search_out);
searchView.setQueryHint(getResources().getText(
R.string.search_messages_hint));
searchView.setIconifiedByDefault(true);
searchView
.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view,
boolean queryTextFocused) {
if (!queryTextFocused) {
// searchView.startAnimation(out);
searchMenuItem.collapseActionView();
} else {
searchView.startAnimation(in);
}
}
});
super.onCreateOptionsMenu(menu, inflater);
}
Update: This appears only in HTC sensation XL with Android 4.0.3, on 4.2 I don't see this problem.
Found the only one solution - set listener in onResume:
#Override
public void onResume() {
super.onResume();
searchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
fpAdapter.getFilter().filter(newText);
} else {
loadData();
}
return false;
}
}); }
use this code
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(final String s) {
if(searchView.getWidth()>0)
{
// your code here
}
return false;
}
});
You can set to use the SearchView only when it's in focus. I had a similar problem where the search was performing in my fragment every time when users resumed it. I solve it with the following method:
-Add a boolean to see when SearchView is in focus:
//by default the SearchView isn't in focus so set it to false.
private boolean shouldSearch = false;
searchView.setOnQueryTextFocusChangeListener((view, hasFocus) -> {
if (hasFocus) {
shouldSearch = true;
} else {
shouldSearch = false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (shouldSearch) {
//do your search here
}
return true;
}
});
This is a known issue. You can fix by checking to see if the search is "iconified" i.e only showing the magnifying glass icon or if it is expanded ( which means the user is interacting with the search view)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
//your code here
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
//this will catch firing event
if (searchView.isIconified) {
return true
}
//your code here
return true
}
})
I'm using SearchView while my activity is extanding Sherlock Library.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater.inflate(R.menu.countriesxml, menu);
SearchView mSearchView = (SearchView) menu.findItem(R.id.searchall)
.getActionView();
mSearchView.setQueryHint("Search for smartphones");
MenuItem menuItem = menu.findItem(R.id.searchall);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub
item.collapseActionView();
indicator.setVisibility(View.VISIBLE);
pager.setVisibility(View.VISIBLE);
lvth.setVisibility(View.GONE);
return true;
}
});
return;
}
Please note that i tried to use OnCloseListener() but it didn't work.
What's wrong?
have a look at your code at this section:
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
Replace it with below code and try again.
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
Try using OnFocusChangeListener instead
menuItem.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean queryTextFocused) {
if (!queryTextFocused) {
// Code here
}
}
});