Android 使用BaseAdapter筛选自定义Listview
我正在尝试筛选一个使用BaseAdapter的自定义Listview,但迄今为止失败得很惨。我想我需要一双新眼睛。我已经通过了各种各样的问题,并在谷歌上搜索了很多。到目前为止,我所尝试的一切都失败了 我正在从Facebook获取用户好友列表,将结果转换到Arraylist中,并将其绑定到适配器的字符串[] 活动代码:Android 使用BaseAdapter筛选自定义Listview,android,android-listview,baseadapter,Android,Android Listview,Baseadapter,我正在尝试筛选一个使用BaseAdapter的自定义Listview,但迄今为止失败得很惨。我想我需要一双新眼睛。我已经通过了各种各样的问题,并在谷歌上搜索了很多。到目前为止,我所尝试的一切都失败了 我正在从Facebook获取用户好友列表,将结果转换到Arraylist中,并将其绑定到适配器的字符串[] 活动代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friends_list);
arrayUID = new ArrayList<String>();
arrayName = new ArrayList<String>();
arrayPicture = new ArrayList<String>();
arrayInfo = new ArrayList<String>();
Bundle extras = getIntent().getExtras();
apiResponse = extras.getString("API_RESPONSE");
graph_or_fql = extras.getString("METHOD");
try {
JAFriends = new JSONArray(apiResponse);
for (int i = 0; i < JAFriends.length(); i++) {
json_data = JAFriends.getJSONObject(i);
// mainAlbumID = json_data.getString("id");
if (json_data.has("uid")) {
String getFriendID = json_data.getString("uid");
arrayUID.add(getFriendID);
} else {
String getFriendID = null;
arrayUID.add(getFriendID);
}
if (json_data.has("name")) {
String getFriendName = json_data.getString("name");
arrayName.add(getFriendName);
} else {
String getFriendName = null;
arrayName.add(getFriendName);
}
if (json_data.has("current_location")) {
try {
JSONObject location = json_data.getJSONObject("current_location");
String friendLocationDetails = location.getString("city") + ", " + location.getString("state");
arrayInfo.add(friendLocationDetails);
} catch (JSONException e) {
arrayInfo.add("");
}
} else {
arrayInfo.add("");
}
if (json_data.has("pic_square")) {
String getFriendPhoto = json_data.getString("pic_square");
arrayPicture.add(getFriendPhoto);
} else {
String getFriendPhoto = null;
arrayPicture.add(getFriendPhoto);
}
}
} catch (JSONException e) {
return;
}
stringUID = new String[arrayUID.size()];
stringUID = arrayUID.toArray(stringUID);
stringName = new String[arrayName.size()];
stringName = arrayName.toArray(stringName);
stringPicture = new String[arrayPicture.size()];
stringPicture = arrayPicture.toArray(stringPicture);
stringInfo = new String[arrayInfo.size()];
stringInfo = arrayInfo.toArray(stringInfo);
listofFriends = (ListView)findViewById(R.id.list);
adapter = new FriendsAdapter(this, stringUID, stringName, stringPicture, stringInfo, arrayName);
listofFriends.setTextFilterEnabled(true);
listofFriends.setAdapter(adapter);
filterText = (EditText) findViewById(R.id.editFilterList);
filterText.addTextChangedListener(filterTextWatcher);
}
private TextWatcher filterTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
// adapter.notifyDataSetChanged();
// listofFriends.setAdapter(adapter);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.friends\u列表);
arrayUID=新的ArrayList();
arrayName=新的ArrayList();
arrayPicture=新的ArrayList();
arrayInfo=新的ArrayList();
Bundle extras=getIntent().getExtras();
apiResponse=extras.getString(“API_响应”);
graph_或_fql=extras.getString(“方法”);
试一试{
JAFriends=新的JSONArray(apiResponse);
for(int i=0;i
我的适配器类:
public class FriendsAdapter extends BaseAdapter implements Filterable {
Activity activity;
String[] fetFriendID;
String[] fetFriendName;
String[] fetFriendPicture;
String[] fetFriendInfo;
List<String> arrayList;
List<String> mOriginalValues;
LayoutInflater inflater = null;
ProfilePictureLoader profileLoader;
FriendsAdapter(Activity a, String[] stringUID, String[] stringName,
String[] stringPicture, String[] stringInfo, ArrayList<String> arrayName) {
activity = a;
fetFriendID = stringUID;
fetFriendName = stringName;
fetFriendPicture = stringPicture;
fetFriendInfo = stringInfo;
arrayList = new ArrayList<String>();
mOriginalValues = arrayName;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
profileLoader = new ProfilePictureLoader(activity.getApplicationContext());
}
public int getCount() {
return mOriginalValues.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView == null)
vi = inflater.inflate(R.layout.friends_item, null);
ImageView imgProfilePicture = (ImageView)vi.findViewById(R.id.profile_pic);
TextView txtFriendsName = (TextView)vi.findViewById(R.id.name);
TextView txtFriendsInfo = (TextView)vi.findViewById(R.id.info);
// SET THE CUSTOM FONTS
Typeface nameHeader = Typeface.createFromAsset(activity.getAssets(), "fonts/museo_slab_700_headers.otf");
Typeface tfContent = Typeface.createFromAsset(activity.getAssets(), "fonts/Cabin-Medium-TTF.ttf");
txtFriendsName.setTypeface(nameHeader);
txtFriendsInfo.setTypeface(tfContent);
txtFriendsName.setText(fetFriendName[position]);
txtFriendsInfo.setText(fetFriendInfo[position]);
if (fetFriendPicture[position] != null){
profileLoader.DisplayImage(fetFriendPicture[position], imgProfilePicture);
}
else if (fetFriendPicture[position] == null) {
imgProfilePicture.setVisibility(View.GONE);
}
return vi;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
arrayList = (List<String>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<String> FilteredArrList = new ArrayList<String>();
if (constraint == null || constraint.length() == 0) {
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i);
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(data);
}
}
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
公共类FriendsAdapter扩展BaseAdapter实现可过滤{
活动;
字符串[]恶臭ID;
字符串[]名称;
字符串[]表示图片;
字符串[]fetFriendInfo;
列表数组列表;
列出mOriginalValues;
排气充气机=空;
ProfilePictureLoader profileLoader;
FriendsAdapter(活动a,字符串[]stringUID,字符串[]stringName,
字符串[]stringPicture、字符串[]stringInfo、ArrayList arrayName){
活动=a;
fetFriendID=stringUID;
fetFriendName=stringName;
fetFriendPicture=stringPicture;
fetFriendInfo=stringInfo;
arrayList=新的arrayList();
mOriginalValues=arrayName;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
profileLoader=新的ProfilePictureLoader(activity.getApplicationContext());
}
public int getCount(){
返回mOriginalValues.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机。充气(R.layout.friends\u项,空);
ImageView imgProfilePicture=(ImageView)vi.findViewById(R.id.profile_pic);
TextView txtFriendsName=(TextView)vi.findViewById(R.id.name);
TextView txtFriendsInfo=(TextView)vi.findViewById(R.id.info);
//设置自定义字体
Typeface name header=Typeface.createFromAsset(activity.getAssets(),“fonts/museo_slab_700_headers.otf”);
字体内容
adapter.getFilter().filter(s);
public Object getItem(int position) {
return mOriginalValues.get(position);
}