刷新(清除并重新填充)listview onTextChanged

时间:2015-05-07 10:56:40

标签: android json listview

代码:

public class TopicsActivity extends ActionBarActivity {


List<String> topics;
Map<String, Topic> topicsMap;
ListView listView;
EditText search;
String searchString;
ArrayAdapter<String> adapter;
String[] topicsArray;

// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

private static String url_topics = "http://192.168.2.102/discussion/get_topics.php";


// restaurants JSONArray
JSONArray Jtopics = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_topics);

    topics = new ArrayList<String>();
    topicsMap = new HashMap<String, Topic>();
    listView = (ListView) findViewById(R.id.topicsList);
    search = (EditText) findViewById(R.id.searchTopic);
    search.addTextChangedListener(searchWatcher);

}

private final TextWatcher searchWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        searchString = "";
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {

        searchString = search.getText().toString();
        new LoadTopics().execute();
    }

    public void afterTextChanged(Editable s) {

    }
};


class LoadTopics extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(TopicsActivity.this);
        pDialog.setMessage("Loading topics. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
    }


    protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("searchString", searchString));

        JSONObject json = jParser.makeHttpRequest(url_topics, "GET", params);

        Log.d("All Topics: ", json.toString());


        try {

            int success = json.getInt("success");

            if (success == 1) {

                Jtopics = json.getJSONArray("topics");

                for (int i = 0; i < Jtopics.length(); i++) {
                    JSONObject c = Jtopics.getJSONObject(i);

                    String title = c.getString("title");
                    String details = c.getString("details");
                    String date = c.getString("date");
                    String username = c.getString("username");

                    Topic t = new Topic(title, details, date, username);

                    topics.add(t.getTitle());

                    topicsMap.put(t.getTitle(), t);

                }
            } else {
            }
        } catch (JSONException e) {

            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {

        pDialog.dismiss();

        runOnUiThread(new Runnable() {
            public void run() {
                topicsArray = new String[topics.size()];
                topics.toArray(topicsArray);

                adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, topicsArray) {

                    @Override
                    public View getView(int position, View convertView,
                                        ViewGroup parent) {
                        View view = super.getView(position, convertView, parent);

                        TextView textView = (TextView) view.findViewById(android.R.id.text1);

                        textView.setTextColor(Color.GREEN);

                        return view;
                    }
                };

                ListView listView = (ListView) findViewById(R.id.topicsList);
                listView.setAdapter(adapter);


            }
        });

    }

}

在我的代码中,我有一个TextWatcher。每次我在EditText中输入文本时,我都希望列表视图刷新/更新/清除并重新填充。这样做的目的是在EditText中搜索主题作为用户权限关键字。代码本身将检索正确的主题但是对于EditText中的每个字母输入,列表将再次填充相同的主题,因此我将重复,然后重复三次等等。我尝试过listview.setAdapter(null),我尝试使用adapter.clear()清除OnTextChanged方法中的适配器。我还尝试了topics.clear()然后adapter.notifyDataSetChanged()。我知道已经存在类似于我的问题,但它们似乎无法解决我的问题。我做错了什么?

谢谢

1 个答案:

答案 0 :(得分:0)

您需要创建一个baseadapter类并实现可过滤。然后在该类中创建一个扩展Filter的私有过滤器类。并在您的代码中创建一个具有该基本适配器类的适配器,并在您的on text更改方法中使用adapter.getFilter()。filter(newText)。