当用户在搜索视图中搜索并且没有找到文件时,我想向用户显示一条消息,然后在文本视图中显示此消息。未找到文件,我该怎么做。搜索代码是完美的工作,但是如果用户搜索列表视图中不可用的内容,然后显示未找到消息,我想这样做。
我的搜索代码
public void filter(String chartext)
{
chartext= chartext.toLowerCase(Locale.getDefault());
heroList.clear();
if(chartext.length()==0)
{
heroList.addAll(arrayList);
}
else
{
for(Pdf pdf : arrayList)
{
if(pdf.getName().toLowerCase(Locale.getDefault()).contains(chartext))
{
heroList.add(pdf);
}
}
}
notifyDataSetChanged();
}
适配器代码
public class PdfAdapter extends BaseAdapter
{
List<Pdf> heroList;
//activity context
LayoutInflater inflater;
Context mcontext;
ArrayList<Pdf>arrayList;
//the layout resource file for the list items
public interface OnItemClickListener{
void onItemClick(int position);
}
private OnItemClickListener mOnItemClickListener;
//constructor initializing the values
public PdfAdapter(Context context, List<Pdf> pdfList,OnItemClickListener mOnItemClickListener) {
this.mcontext = context;
this.heroList = pdfList;
inflater = LayoutInflater.from(mcontext);
this.arrayList= new ArrayList<Pdf>();
this.arrayList.addAll(pdfList);
this.mOnItemClickListener =mOnItemClickListener;
// mOnItemClickListener = listener;
}
@Override
public int getCount() {
return heroList.size();
}
@Override
public Object getItem(int i) {
return heroList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_layout, null);
// LayoutInflater layoutInflater = LayoutInflater.from(context);
// Typeface custom_font = Typeface.createFromAsset(getContext().getAssets(),"DroidSans-Bold.ttf");
holder.textViewName = convertView.findViewById(R.id.textViewName);
holder.textViewurl = convertView.findViewById(R.id.textViewUrl);
holder.download = convertView.findViewById(R.id.downloadimage);
// textViewName.setTypeface(custom_font);
// Button buttonpass = view.findViewById(R.id.password);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
// Pdf hero = heroList.get(position);
holder.textViewName.setText(heroList.get(position).getName());
holder.textViewurl.setText(heroList.get(position).getUrl());
holder.download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnItemClickListener.onItemClick(position);
}
});
return convertView;
}
public class ViewHolder
{
TextView textViewName,textViewurl,note;
ImageView download;
}
public void filter(String chartext)
{
chartext= chartext.toLowerCase(Locale.getDefault());
heroList.clear();
if(chartext.length()==0)
{
heroList.addAll(arrayList);
}
else
{
for(Pdf pdf : arrayList)
{
if(pdf.getName().toLowerCase(Locale.getDefault()).contains(chartext))
{
heroList.add(pdf);
}
}
}
notifyDataSetChanged();
}
}
答案 0 :(得分:0)
您可以使用名为“ Toast”的组件,here you can find the guidelines。
吐司可在一个小弹出窗口中提供有关操作的简单反馈。它仅填充消息所需的空间量,并且当前活动保持可见和交互式。超时后,吐司自动消失。
一个吐司的例子:
Context context = getApplicationContext();
CharSequence text = "No file found!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
您可以像这样压缩所有内容:
Toast.makeText(getApplicationContext(),"No file found!",Toast.LENGTH_SHORT).show();
因此在您的应用程序中将如下所示:
if (heroList.size() == 0) {
Toast.makeText(getApplicationContext(),"No file found!",Toast.LENGTH_SHORT).show();
}