如何返回"没有搜索结果"在Android中找到

时间:2014-01-03 18:00:40

标签: android listview textview

我的搜索是使用listView完成的,所以当用户键在firstname的第一个字母时,它会返回结果,如何让它在Android中显示“找不到结果”?  请指教,我是否需要创建一个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 个答案:

答案 0 :(得分:0)

您可以创建Toast,而不是创建TextView,它实际上是一个弹出窗口。您可以像这样创建它:

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

如果您希望弹出窗口持续更长时间,可以将最后一个参数更改为Toast.LENGTH_LONG。

答案 1 :(得分:0)

在我的应用程序中,对于这种情况,我在列表中有一个具有特殊类型'EMPTY_RESULT'的项目。

基本上,在输出EMPTY结果后,生成一个特定类型为'EMPTY_RESUT'的适配器项(表示getCount()== 1),并通知数据集更改。

然后,当调用getView时,返回空项,当然你可以为它设计一些UI来呈现空结果,例如图标和文本“找不到任何东西”。