How to return "no search result" found in Android

1.5k Views Asked by At

My Search is done using a listView, so when the user key in the the first letter in the firstname, it will return the result, how can I make it display "no result found" in Android? Please advise, do I need to create a a TextView?

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context mContext;
    LayoutInflater inflater;
    private List<User> namelist = null;
    private ArrayList<User> arraylist;

    public ListViewAdapter(Context context,
            List<User> worldpopulationlist) {
        mContext = context;
        this.namelist = worldpopulationlist;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<User>();
        this.arraylist.addAll(namelist);
    }

    public class ViewHolder {
        TextView firstname;
        TextView lastname;
        TextView position;
        TextView company;
        ImageView pix;
        TextView searchresult;
    }

    @Override
    public int getCount() {
        return namelist.size();
    }

    @Override
    public User getItem(int position) {
        return namelist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.listview_item, null);
            // Locate the TextViews in listview_item.xml
            holder.firstname = (TextView) view.findViewById(R.id.firstname);
            holder.lastname = (TextView) view.findViewById(R.id.lastname);
            holder.position= (TextView) view.findViewById(R.id.position);
            holder.company = (TextView) view.findViewById(R.id.company);

            // Locate the ImageView in listview_item.xml
            holder.pix = (ImageView) view.findViewById(R.id.pix);
            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.firstname.setText(namelist.get(position).getFirstName());
        holder.lastname.setText(namelist.get(position).getLastName());
        holder.position.setText(namelist.get(position).getDesignation());
        holder.company.setText(namelist.get(position).getCompanyName());
        // Set the results into ImageView
        holder.pix.setImageResource(namelist.get(position).getProfileImage());
        // Listen for ListView Item Click
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Send single item click data to SingleItemView Class
                Intent intent = new Intent(mContext, SingleItemView.class);
                // Pass all data country
                intent.putExtra("firstname",
                        (namelist.get(position).getFirstName()));
                intent.putExtra("lastname",
                        (namelist.get(position).getLastName()));
                // Pass all data rank
                intent.putExtra("position",
                        (namelist.get(position).getDesignation()));
                // Pass all data population
                intent.putExtra("company",
                        (namelist.get(position).getCompanyName()));
                // Pass all data flag
                intent.putExtra("pix",
                        (namelist.get(position).getProfileImage()));
                // Start SingleItemView Class
                mContext.startActivity(intent);
            }
        });

        return view;
    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        namelist.clear();
        if (charText.length() == 0) {
            namelist.addAll(arraylist);
        } else {
            for (User wp : arraylist) {
                if (wp.getFirstName().toLowerCase(Locale.getDefault()).contains(charText)||wp.getLastName().toLowerCase(Locale.getDefault()).contains(charText)     ) {
                    namelist.add(wp);
                }
                else
                {

                    //what should I enter here?
                }

            }
        }
        notifyDataSetChanged();
    }



}
2

There are 2 best solutions below

0
windkiosk On BEST ANSWER

In my App, for this case, I have an item in list which has a special type 'EMPTY_RESULT'.

Basically, after getting out result which is EMPTY, generate an item with specific type 'EMPTY_RESUT'in Adapter (means getCount() == 1 ), and notify dataset change.

Then, when getView be called, return the empty item and certainly you could design some UI for it to present empty result, such as an icon and text 'nothing found'.

0
epsilondelta On

Instead of creating a TextView, you can create a Toast, which is essentially a popup. You can create it like this:

Toast.makeText(mContext, "No Results Found", Toast.LENGTH_SHORT).show();

You can change the last parameter to Toast.LENGTH_LONG if you want the popup to last longer.