0

我现在正在努力解决这个问题。我正在用edittext构建一个可过滤的列表。该列表最初填充得很好,但是一旦我在编辑文本字段中输入了一些内容。整个列表都消失了,并且没有返回任何结果。以下是我的代码。

注意:我的代码主要基于 sacoskun 的帖子

ThreedListViewActivity.java

public class ThreedListViewActivity extends ActionBarActivity
{
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";

ListView list;
ThreedListAdapterFilterable adapter;
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
EditText filterText = null;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.threed_listview);
    setTitle(R.string.view_3d);
    list=(ListView)findViewById(R.id.list);
    adapter = new ThreedListAdapterFilterable(this, songsList);

    new MyAsyncTask().execute();

    filterText = (EditText) findViewById(R.id.search_box);
    filterText.addTextChangedListener(filterTextWatcher);

    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        }
    });
}

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        adapter.getFilter().filter(s.toString());
    }

};

public class MyAsyncTask extends AsyncTask<Void,Void,Void>{

    private final ProgressDialog dialog=new ProgressDialog(ThreedListViewActivity.this);

    @Override
    protected Void doInBackground(Void... params) {

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }
        return null;
    }

    @Override
    protected void onPreExecute()
    {
        dialog.setMessage("Loading ...");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected void onPostExecute(Void result)
    {
        if(dialog.isShowing() == true)
        {
            dialog.dismiss();
        }
         // Getting adapter by passing xml data ArrayList

        list.setAdapter(adapter);

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.action_items, menu);

    return true;
}

}

ThreedListAdapterFilterable.java

public class ThreedListAdapterFilterable extends BaseAdapter implements Filterable {

private Activity activity;
ArrayList<HashMap<String, String>> mDataShown;
ArrayList<HashMap<String, String>> mAllData;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public ThreedListAdapterFilterable(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    mDataShown= (ArrayList<HashMap<String, String>>) d;
    mAllData = (ArrayList<HashMap<String, String>>) mDataShown.clone();
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return mDataShown.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

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

        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.threed_listrow, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
        TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = mDataShown.get(position);

        // Setting all values in listview
        title.setText(song.get(ThreedListViewActivity.KEY_TITLE));
        artist.setText(song.get(ThreedListViewActivity.KEY_ARTIST));
        duration.setText(song.get(ThreedListViewActivity.KEY_DURATION));
        imageLoader.DisplayImage(song.get(ThreedListViewActivity.KEY_THUMB_URL), thumb_image);
        return vi;
}

public Filter getFilter() {
     Filter nameFilter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
           public String convertResultToString(Object resultValue) {
                return ((HashMap<String, String>)(resultValue)).get(ThreedListViewActivity.KEY_TITLE);
           }

            @Override
            protected FilterResults performFiltering(CharSequence s) {

                 if(s != null)
                    {
                     ArrayList<HashMap<String, String>> tmpAllData = mAllData;
                     ArrayList<HashMap<String, String>> tmpDataShown = mDataShown;
                     tmpDataShown.clear();  
                     for(int i = 0; i < tmpAllData.size(); i++)
                     {
                      if(tmpAllData.get(i).get(ThreedListViewActivity.KEY_TITLE).toLowerCase().startsWith(s.toString().toLowerCase()))
                      {
                       tmpDataShown.add(tmpAllData.get(i));
                      }
                     }

                     FilterResults filterResults = new FilterResults();
                     filterResults.values = tmpDataShown;
                     filterResults.count = tmpDataShown.size();
                     return filterResults;
                    }
                    else
                    {
                     return new FilterResults();
                    }
            }

            @Override
               protected void publishResults(CharSequence s,
                 FilterResults results) {
                if(results != null && results.count > 0)
                {
                 notifyDataSetChanged();
                }
               }};
               return nameFilter;
    }
}

编辑: 这是一个更新的适配器,将过滤我的列表。但是,当我退格输入文本中的文本时,它不会更新我的列表。

public class ProjectListAdapter extends BaseAdapter implements Filterable{

private Activity activity;
ArrayList<HashMap<String, String>> mDataShown;
ArrayList<HashMap<String, String>> mAllData;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 
HashMap<String, String> song = new HashMap<String, String>();
ArrayList<HashMap<String, String>> filteredItems;

public ProjectListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {

    activity = a;
    mDataShown= (ArrayList<HashMap<String, String>>) d;
    mAllData = (ArrayList<HashMap<String, String>>) mDataShown.clone();
    filteredItems = mDataShown;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

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

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return 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;

    if(convertView==null){
        vi=new View(activity);
        vi = inflater.inflate(R.layout.recents_listrow, null);
    }else{
        vi = (View)convertView;
    }
    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.company); // artist name
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
    title.setText(filteredItems.get(position).get(RecentsFragment.KEY_TITLE));
    artist.setText(filteredItems.get(position).get(RecentsFragment.KEY_COMPANY));
    imageLoader.DisplayImage(filteredItems.get(position).get(RecentsFragment.KEY_THUMB_URL), thumb_image);

    return vi;

}

public Filter getFilter() {
     Filter nameFilter = new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {


                if(constraint != null && constraint.toString().length() > 0) {
                    constraint = constraint.toString().toUpperCase();
                    ArrayList<HashMap<String, String>> filt = mDataShown;
                    ArrayList<HashMap<String, String>> tmpItems = mAllData;
                    filt.clear();
                                    for(int i = 0; i < tmpItems.size(); i++) {

                                      if(tmpItems.get(i).get(RecentsFragment.KEY_TITLE).toLowerCase().startsWith(constraint.toString().toLowerCase()))
                                      {
                                          filt.add(tmpItems.get(i));
                                      }
                                    }
                                    FilterResults filterResults = new FilterResults();
                                    filterResults.count = filt.size();
                                    filterResults.values = filt;
                                    return filterResults;
                               }else{
                                   return new FilterResults();
                                  }

            }

            @Override
               protected void publishResults(CharSequence constraint,
                 FilterResults results) {

                mDataShown = (ArrayList<HashMap<String, String>>)results.values;

                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }

               }
            };

               return nameFilter;
 }

}

我正在尝试实现的列表片段:RecentsFragment

public class RecentsFragment extends ListFragment {

// All static variables
static final String URL = "http://www.sundancepost.com/ivue/projects.xml";
// XML node keys
static final String KEY_PROJECT = "project"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_COMPANY = "company";
static final String KEY_THUMB_URL = "thumb_url";
int mCurCheckPosition = 0;

ListView list;
ProjectListAdapter adapter;
ArrayList<HashMap<String, String>> projectsList = new ArrayList<HashMap<String, String>>();
EditText filterText = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);

}

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.recents_list, container, false);
     list = (ListView)view.findViewById(android.R.id.list);
     filterText = (EditText)view.findViewById(R.id.filter_box);

     return view;
 }

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if(null == savedInstanceState){

        ConnectivityManager cMgr = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cMgr.getActiveNetworkInfo() != null && cMgr.getActiveNetworkInfo().isConnectedOrConnecting()) {

            new MyAsyncTask().execute();

        } else {
             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
             builder.setMessage("Please check your internet connection");
             builder.setTitle("Failed to download resources");
             builder.setCancelable(false);
             builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           return;
                       }
                   });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }

    adapter = new ProjectListAdapter(getActivity(), projectsList);
    list.requestFocus();
    list.setTextFilterEnabled(true);
    filterText.addTextChangedListener(filterTextWatcher);

    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(getActivity(), DetailsActivity.class);
            intent.putExtra("title",projectsList.get(position).get(KEY_TITLE));
            intent.putExtra("company", projectsList.get(position).get(KEY_COMPANY));

            startActivity(intent);

        }
    });

}

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putInt("workaround", mCurCheckPosition);
}

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        adapter.getFilter().filter(s);
    }

};

@Override
public void onDestroy() {
    super.onDestroy();
    filterText.removeTextChangedListener(filterTextWatcher);
}

public class MyAsyncTask extends AsyncTask<Void,Void,Void>{

    private final ProgressDialog recents_dialog = new ProgressDialog(getActivity());

    @Override
    protected Void doInBackground(Void... params) {

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_PROJECT);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_COMPANY, parser.getValue(e, KEY_COMPANY));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            projectsList.add(map);
        }
        return null;
    }

    @Override
    protected void onPreExecute()
    {
        projectsList.clear();
        recents_dialog.setMessage("Loading ...");
        recents_dialog.show();
        recents_dialog.setCancelable(false);
    }

    @Override
    protected void onPostExecute(Void result)
    {
        if(recents_dialog.isShowing() == true)
        {
            recents_dialog.dismiss();
        }
         // Getting adapter by passing xml data ArrayList

        list.setAdapter(adapter);

    }
}


}
4

2 回答 2

0

公共类 FriendsListActivity 扩展 Activity 实现 OnClickListener { private Handler mHandler; 静态最终字符串 KEY_ID = "id"; 静态最终字符串 KEY_NAME = "名称"; 静态最终字符串 KEY_INSTALLED = "已安装"; 静态最终字符串 KEY_THUMB_URL = "图片"; 受保护的 ListView 好友列表;受保护的静态 JSONArray jsonArray; FriendsListAdapter 适配器;ArrayList>好友列表;编辑文本过滤器文本;字符串 apiResponse;按键播放、邀请;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.friends_list);
    friendslist = new ArrayList<HashMap<String, String>>();
    mHandler = new Handler();
    ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
    actionBar.setTitle("Facebook Friends");
    actionBar.setHomeAction(new IntentAction(this, TruthorDareActivity
            .createIntent(this), R.drawable.ic_home));
    friendsList = (ListView) findViewById(R.id.friendsList);
    filterText = (EditText) findViewById(R.id.searchBox);
    filterText.addTextChangedListener(filterTextWatcher);
    friendsList.setTextFilterEnabled(true);
    friendsList.requestFocus();
    play = (Button) findViewById(R.id.play);
    invite = (Button) findViewById(R.id.invite);
    play.setOnClickListener(this);
    invite.setOnClickListener(this);
    Bundle extras = getIntent().getExtras();
    apiResponse = extras.getString("API_RESPONSE");
    callPlayList();
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}

@Override
protected void onResume() {
    super.onResume();
    friendsList.requestFocus();
}

public void callPlayList() {
    try {
        jsonArray = new JSONObject(apiResponse).getJSONArray("data");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json_data = jsonArray.getJSONObject(i);
            if (json_data.has("installed")
                    && json_data.getBoolean("installed") == true) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(KEY_ID, json_data.getString("id"));
                map.put(KEY_NAME, json_data.getString("name"));
                map.put(KEY_THUMB_URL, json_data.getString("picture"));
                map.put(KEY_INSTALLED, json_data.getString("installed"));
                friendslist.add(map);
            }
        }

    } catch (JSONException e) {
        showToast("Error: " + e.getMessage());
        return;
    }
    adapter = new FriendsListAdapter(FriendsListActivity.this, friendslist);
    friendsList.setAdapter(adapter);

}

public void callInviteList() {
    try {
        jsonArray = new JSONObject(apiResponse).getJSONArray("data");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json_data = jsonArray.getJSONObject(i);
            if (!json_data.has("installed")) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(KEY_ID, json_data.getString("id"));
                map.put(KEY_NAME, json_data.getString("name"));
                map.put(KEY_THUMB_URL, json_data.getString("picture"));
                map.put(KEY_INSTALLED, "false");
                friendslist.add(map);
            }
        }

    } catch (JSONException e) {
        showToast("Error: " + e.getMessage());
        return;
    }
    adapter = new FriendsListAdapter(FriendsListActivity.this, friendslist);
    friendsList.setAdapter(adapter);
}

public void clearAdapter(String params) {
    friendslist.clear();
    adapter.notifyDataSetChanged();
    adapter.notifyDataSetInvalidated();
    if (params.equals("invite")) {
        callInviteList();
    } else {
        callPlayList();
    }

}

public class PostDialogListener extends BaseDialogListener {
    @Override
    public void onComplete(Bundle values) {
        final String postId = values.getString("post_id");
        if (postId != null) {
            showToast("Message posted on the wall.");
        } else {
            showToast("No message posted on the wall.");
        }
    }
}

public void showToast(final String msg) {
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            Toast toast = Toast.makeText(FriendsListActivity.this, msg,
                    Toast.LENGTH_LONG);
            toast.show();
        }
    });
}

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        adapter.getFilter().filter(s);
    }

};

@Override
protected void onDestroy() {
    super.onDestroy();
    filterText.removeTextChangedListener(filterTextWatcher);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.play:
        clearAdapter("play");
        break;
    case R.id.invite:
        clearAdapter("invite");
        break;
    default:
        break;
    }

}

}

下一部分是我在单独的类文件中使用的适配器类。

public class FriendsListAdapter extends BaseAdapter implements Filterable {

private Activity activity;
public ArrayList<HashMap<String, String>> data;
public ArrayList<HashMap<String, String>> allData;
public HashMap<String, String> friends;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
private ItemsFilter mFilter;
static SharedPreferences TODLoginPrefs;

@SuppressWarnings({ "unchecked", "static-access" })
public FriendsListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = (ArrayList<HashMap<String, String>>) d;
    allData = (ArrayList<HashMap<String, String>>) data.clone();
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
    TODLoginPrefs = activity.getSharedPreferences("LogInSession",
            activity.MODE_PRIVATE);

}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.friends_list_row, null);

    TextView user = (TextView) vi.findViewById(R.id.gname);
    Button play = (Button) vi.findViewById(R.id.btnPlay);
    Button invite = (Button) vi.findViewById(R.id.btnInvite);
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);

    friends = new HashMap<String, String>();
    friends = data.get(position);

    user.setText(friends.get(FriendsListActivity.KEY_NAME));
    imageLoader.DisplayImage(
            friends.get(FriendsListActivity.KEY_THUMB_URL), thumb_image);
    final String friendrId = (friends.get(FriendsListActivity.KEY_ID));
    final String name = (friends.get(FriendsListActivity.KEY_NAME));
    final String installed = (friends
            .get(FriendsListActivity.KEY_INSTALLED));
    if (installed.equals("false")) {
        play.setVisibility(View.GONE);
        invite.setVisibility(View.VISIBLE);
    }
    play.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(activity)
                    .setTitle("Create game with " + name + "?")
                    .setPositiveButton(R.string.yes,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    UserFunction userFunction = new UserFunction();
                                    JSONObject json_game = userFunction
                                            .newGame(
                                                    activity,
                                                    "Facebook",
                                                    TODLoginPrefs
                                                            .getString(
                                                                    "UserID",
                                                                    ""),
                                                    friendrId, null, null);
                                    if (json_game.has("success")) {
                                        activity.startActivity(new Intent(
                                                activity,
                                                TruthorDareActivity.class));
                                    } else {
                                        try {
                                            Toast.makeText(
                                                    activity,
                                                    json_game
                                                            .getString("error_msg"),
                                                    Toast.LENGTH_SHORT)
                                                    .show();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }

                            }).setNegativeButton(R.string.no, null).show();

        }
    });
    invite.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new AlertDialog.Builder(activity)
                    .setTitle("Invite " + name + "?")
                    .setPositiveButton(R.string.yes,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    UserFunction userFunction = new UserFunction();
                                    JSONObject json_game = userFunction
                                            .newInvite(
                                                    activity,
                                                    "Facebook",
                                                    TODLoginPrefs
                                                            .getString(
                                                                    "UserID",
                                                                    ""),
                                                    friendrId, null, null);
                                    if (json_game.has("success")) {
                                        Bundle params = new Bundle();
                                        params.putString("to", friendrId);
                                        params.putString(
                                                "message",
                                                activity.getResources()
                                                        .getString(
                                                                R.string.app_request_message));
                                        Utilities.facebook.dialog(activity,
                                                "apprequests", params,
                                                new AppRequestsListener());
                                    } else {
                                        try {
                                            Toast.makeText(
                                                    activity,
                                                    json_game
                                                            .getString("error_msg"),
                                                    Toast.LENGTH_SHORT)
                                                    .show();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }

                            }).setNegativeButton(R.string.no, null).show();

        }
    });
    return vi;
}

@Override
public Filter getFilter() {
    if (mFilter == null) {
        mFilter = new ItemsFilter();
    }
    return mFilter;
}

private class ItemsFilter extends Filter {
    @SuppressWarnings("unchecked")
    @Override
    public String convertResultToString(Object resultValue) {
        return ((HashMap<String, String>) (resultValue))
                .get(FriendsListActivity.KEY_NAME);
    }

    @Override
    protected FilterResults performFiltering(CharSequence s) {

        if (s != null) {
            ArrayList<HashMap<String, String>> tmpAllData = allData;
            ArrayList<HashMap<String, String>> tmpDataShown = data;
            tmpDataShown.clear();
            for (int i = 0; i < tmpAllData.size(); i++) {
                if (tmpAllData.get(i).get(FriendsListActivity.KEY_NAME)
                        .toLowerCase()
                        .startsWith(s.toString().toLowerCase())) {
                    tmpDataShown.add(tmpAllData.get(i));
                }
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = tmpDataShown;
            filterResults.count = tmpDataShown.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

    @SuppressWarnings("unchecked")
    protected void publishResults(CharSequence prefix, FilterResults results) {
        data = (ArrayList<HashMap<String, String>>) results.values;
        if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }
    }
}

public class AppRequestsListener extends BaseDialogListener {
    @Override
    public void onComplete(Bundle values) {
        Toast.makeText(activity, "App request sent", Toast.LENGTH_SHORT)
                .show();
        activity.startActivity(new Intent(activity,
                TruthorDareActivity.class));
    }

    @Override
    public void onFacebookError(FacebookError error) {
        Toast.makeText(activity, "Facebook Error: " + error.getMessage(),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel() {
        Toast.makeText(activity, "App request cancelled",
                Toast.LENGTH_SHORT).show();
    }
}

}

于 2012-07-04T09:06:43.200 回答
0

在 getFilter() 方法中试试这个

@Override protected void publishResults(CharSequence 约束,FilterResults 结果) {

            mDataShown = (ArrayList<HashMap<String, String>>)results.values;
            ProjectListAdapter.this.setListData(mDataShown);
            ProjectListAdapter.this.notifyDataSetChanged();
        };

并在退格上刷新列表

私有 TextWatcher filterTextWatcher = new TextWatcher() {

public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
}

public void onTextChanged(CharSequence s, int start, int before,
        int count) {
    if(s.toString().equalsIgnoreCase(""))
    {
     adapter.setListData(projectListdata)
     adapter.notifyDataSetChanged();
    }
    else
     adapter.getFilter().filter(s);
}

};

于 2012-09-24T13:22:46.013 回答