2014-03-03 69 views
0

如何使其顯示「未找到結果」。這裏的代碼..如何顯示「無結果」 - ANDROID

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填充的基礎上標籤。

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(); 
     } 
    }; 
} 
} 
+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); 
+0

看起來你可以在'performFiltering()'方法的最後調用'Toast.makeText()',假設你想要顯示Toast。唯一的問題是你需要確保你有一個有效的'Context',因爲你只在你的一個構造函數中存儲'Context'引用。 –