我有一个列表,其中包含我的应用用户的 Facebook 好友列表。上面Listview
我有一个EditText
按字母顺序搜索这个朋友列表。EditText
当.I中的文本更改为列表时,我无法重新生成BaseAdapter
列表。这是我的代码:
public class FriendListActivity extends Activity implements OnItemClickListener
在这里面:
editTextFriendSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// here i use filter in listAdapter.
friendListAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
});
这是使用过滤器更新的我的适配器
public class FriendListAdapter extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
FriendListActivity friendsList;
public FriendListAdapter(FriendListActivity friendsList) {
this.friendsList = friendsList;
if (Utility.model == null) {
Utility.model = new FriendsGetProfilePics();
}
Utility.model.setListener(this);
mInflater = LayoutInflater.from(friendsList.getBaseContext());
}
@Override
public int getCount() {
return jsonArray.length();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(position);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
View hView = convertView;
if (convertView == null) {
hView = mInflater.inflate(R.layout.row_view, null);
ViewHolder holder = new ViewHolder();
holder.profile_pic = (ImageView) hView
.findViewById(R.id.profile_pic);
holder.name = (TextView) hView.findViewById(R.id.name);
hView.setTag(holder);
}
ViewHolder holder = (ViewHolder) hView.getTag();
try {
if (graph_or_fql.equals("graph")) {
holder.profile_pic.setImageBitmap(Utility.model.getImage(
jsonObject.getString("id"),
jsonObject.getString("picture")));
} else {
holder.profile_pic.setImageBitmap(Utility.model.getImage(
jsonObject.getString("uid"),
jsonObject.getString("pic_square")));
}
holder.name.setText(jsonObject.getString("name"));
} catch (Exception e) {
}
return hView;
}
// update : Now i use filter here..
@Override
public Filter getFilter() {
if (newfilter == null) {
newfilter = new Filter() {
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
Log.d("----Filter----", "publishResults");
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(
CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
Log.d("Filter constraints", "constraint : "
+ constraint);
// here i create json array object but
// not sure am i walking right way ??
ArrayList<JSONObject> jsonObjects = new ArrayList<JSONObject>();
if (constraint != null && constraint.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject o = (JSONObject) jsonArray
.get(i);
if (o.getString("name").toLowerCase()
.contains(constraint)) {
Log.d("----Results-----", "equals : "
+ o.getString("name"));
jsonObjects.add(o);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
FilterResults newFilterResults = new FilterResults();
newFilterResults.count = jsonObjects.size();
newFilterResults.values = jsonObjects;
Log.d("----Count----", " " + newFilterResults.count
+ " " + newFilterResults.values);
return newFilterResults;
}
};
}
return newfilter;
}
}
class ViewHolder {
ImageView profile_pic;
TextView name;
}
现在我可以在 Log cat 中获取搜索列表,但未填充 Listview。?