如何显示“无结果” - ANDROID

时间:2014-03-03 02:57:41

标签: android android-listview textview

如何让它显示“找不到结果”。这是代码..

final BinderActorData bindingData = new BinderActorData(this,
                actorDataCollection);
list = (ListView) findViewById(R.id.list);
// Adding items to listview
inputSearch = (EditText) findViewById(R.id.inputSearch);

list.setAdapter(bindingData);

/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
        int arg3) {
    // When user changed the Text
    bindingData.getFilter().filter(cs);
    list.setAdapter(bindingData);
}

然后onClick我正在使用以下代码:

// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

Intent i = new Intent();
i.setClass(BrowseActors.this, ViewActor.class);
int t = position;
// parameters
i.putExtra("position", String.valueOf(position + 1));

 /*
  * selected item parameters
 */

  i.putExtra("name",
                actorDataCollection.get(position).get(KEY_NAME));
  i.putExtra("icon",
                actorDataCollection.get(position).get(KEY_ICON));
  i.putExtra("career",
                actorDataCollection.get(position).get(KEY_CAREER));

        // start the sample activity
        startActivity(i);
    }
});

这里的actorDataCollection是我从外部XML填充的,基于Tags。

List<HashMap<String, String>> actorDataCollection;

这是我的自定义BaseAdapter:

public class BinderActorData extends BaseAdapter implements Filterable {
static final String KEY_TAG = "ActorData"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_ICON = "icon";
static final String KEY_CAREER = "career";

LayoutInflater inflater;

private List<HashMap<String, String>> actorDataCollection;
private List<HashMap<String, String>> filteredData;
ViewHolder holder;
private Context context;

public BinderActorData(Context context) {
    this.context = context;
}

public BinderActorData() {
    // TODO Auto-generated constructor stub
}

public BinderActorData(Activity act, List<HashMap<String, String>> map) {

    this.actorDataCollection = map;
    this.filteredData = map;
    inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return filteredData.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return filteredData.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    if (convertView == null) {

        vi = inflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();
        holder.name = (TextView) vi.findViewById(R.id.name); // type
        holder.actorImage = (ImageView) vi.findViewById(R.id.list_image); // thumb
                                                                            // image
        vi.setTag(holder);
    } else {

        holder = (ViewHolder) vi.getTag();
    }


    // Get data from filtered Data
    holder.name.setText(filteredData.get(position).get(KEY_NAME));
    // Setting Thumbnail image
    String imageUri = filteredData.get(position).get(KEY_ICON);

    AssetActivity a = new AssetActivity();
    context = vi.getContext();
    Drawable image = a.getImage(imageUri, context);
    holder.actorImage.setImageDrawable(image);

    return vi;
}

static class ViewHolder {
    TextView name;
    ImageView actorImage;
}

@Override
public Filter getFilter() {
    return new Filter() {
        @SuppressLint("DefaultLocale")
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults results = new FilterResults();
            // If there's nothing to filter on, return the original data for
            // your list
            if (charSequence == null || charSequence.length() == 0) {
                results.values = actorDataCollection;
                results.count = actorDataCollection.size();
            } else {
                List<HashMap<String, String>> filterResultsData = new      ArrayList<HashMap<String, String>>();

                for (int i = 0; i < actorDataCollection.size(); i++) {
                    if (actorDataCollection.get(i).get(KEY_NAME)
                            .toLowerCase().toString()
                            .indexOf(charSequence.toString().toLowerCase()) >= 0) {
                        filterResultsData.add(actorDataCollection.get(i));
                    }
                }

                results.values = filterResultsData;
                results.count = filterResultsData.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence charSequence,
                FilterResults filterResults) {
            filteredData = (List<HashMap<String, String>>) filterResults.values;
            notifyDataSetChanged();
        }
    };
}
}

1 个答案:

答案 0 :(得分:1)

ListViewsetEmptyView(View)方法。

虽然有很多方法可以将View作为空视图传入,但我通常会在包含TextView的XML布局中添加ListView,然后设置它像这样:

View emptyView = findViewById(R.id.my_empty_view);
ListView listView = (ListView) findViewById(R.id.my_list_view);
listView.setEmptyView(emptyView);