when I type & search item name in Custom Search view basically its an Editext which in I am typing searching text, but after searching the item for example banana it properly filter and shows banana item but when I click on that it shows the wrong position toast plz somebody, anybody help me to rid out of this bug
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<AdapterItem> mAdapterList;
TextView Headertv;
LinearLayout SearchView;
EditText editTextSearch;
ImageView ivClearText;
RecyclerView recyclerView;
CustomAdapter adapter;
public String[] fruitnames,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createAList();
buildRecyclerView();
Headertv = (TextView) findViewById(R.id.app_name_head);
SearchView = (LinearLayout) findViewById(R.id.search_bar);
editTextSearch = (EditText) findViewById(R.id.edt_search_text);
ivClearText = (ImageView) findViewById(R.id.iv_clear_text);
editTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
editTextSearch.addTextChangedListener(searchViewTextWatcher);
ivClearText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editTextSearch.setText("");
}
});
}
public void search_names(View view) {
editTextSearch.requestFocus();
editTextSearch.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editTextSearch, InputMethodManager.SHOW_FORCED);
Headertv.setVisibility(View.GONE);
SearchView.setVisibility(View.VISIBLE);
}
public void Close_Search_View(View view) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
editTextSearch.setText("");
Headertv.setVisibility(View.VISIBLE);
SearchView.setVisibility(View.GONE);
}
private void filter(String text) {
ArrayList<AdapterItem> filteredList = new ArrayList<>();
for (final AdapterItem item : mAdapterList) {
if (item.getText1().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
adapter.filterList(filteredList);
}
TextWatcher searchViewTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().trim().length() == 0) {
ivClearText.setVisibility(View.GONE);
} else {
ivClearText.setVisibility(View.VISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable editable) {
filter(editable.toString());
}
};
private void createAList() {
fruitnames = new String[]{"apple","mango", "cherry","banana","kiwi"};
mAdapterList = new ArrayList<>();
for(int i =0;i<fruitnames .length;i++)
{
mAdapterList.add(new AdapterItem( fruitnames[i]));
}
}
private void buildRecyclerView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
adapter = new CustomAdapter(mAdapterList);
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(new ItemTouchListener(getApplicationContext(), recyclerView, new ItemTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
AdapterItem currentItem = mAdapterList.get(position);
if (currentItem.getText1().contains(fruitnames[position])) {
Toast.makeText(getApplicationContext(), "name displayed:" + currentItem.getText1() , Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLongClick(View view, int position) {
}
}));
}
}
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.AViewHolder> {
private ArrayList<AdapterItem> mAdapterList;
public static class AViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView1;
public AViewHolder(View itemView) {
super(itemView);
mTextView1 = itemView.findViewById(R.id.textViewName);
}
}
public CustomAdapter(ArrayList<AdapterItem> adapterList) {
mAdapterList = adapterList;
}
@Override
public AViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview,
parent, false);
return new AViewHolder(v);
}
@Override
public void onBindViewHolder(AViewHolder holder, int position) {
AdapterItem currentItem = mAdapterList.get(position);
holder.mTextView1.setText(currentItem.getText1());
}
@Override
public int getItemCount() {
return mAdapterList.size();
}
public void filterList(ArrayList<AdapterItem> filteredList) {
mAdapterList = filteredList;
notifyDataSetChanged();
}
}
ItemTouchListener.java
public class ItemTouchListener implements RecyclerView.OnItemTouchListener
{
private GestureDetector gestureDetector;
private ClickListener clickListener;
public ItemTouchListener(Context context, final RecyclerView recyclerView,
final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new
GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child,
recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null &&
gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
See Question&Answers more detail:
os