Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 刷新(清除并重新填充)listview onTextChanged_Android_Json_Listview - Fatal编程技术网

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

Android 刷新(清除并重新填充)listview onTextChanged,android,json,listview,Android,Json,Listview,代码: 公共类TopicsActivity扩展了ActionBarActivity{ 列出主题; 地图主题地图; 列表视图列表视图; 编辑文本搜索; 字符串搜索字符串; 阵列适配器; 字符串[]topicsArray; //进度对话框 私人对话; //创建JSON解析器对象 JSONParser jParser=新的JSONParser(); 私有静态字符串url_主题=”http://192.168.2.102/discussion/get_topics.php"; //杰索纳雷餐厅 JSON

代码:

公共类TopicsActivity扩展了ActionBarActivity{
列出主题;
地图主题地图;
列表视图列表视图;
编辑文本搜索;
字符串搜索字符串;
阵列适配器;
字符串[]topicsArray;
//进度对话框
私人对话;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
私有静态字符串url_主题=”http://192.168.2.102/discussion/get_topics.php";
//杰索纳雷餐厅
JSONArray Jtopics=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_主题);
topics=newarraylist();
topicsMap=newhashmap();
listView=(listView)findViewById(R.id.topicsList);
search=(EditText)findViewById(R.id.searchTopic);
search.addTextChangedListener(searchWatcher);
}
private final TextWatcher searchWatcher=新TextWatcher(){
更改前文本之前的公共void(字符序列s、int start、int count、int after){
searchString=“”;
}
public void onTextChanged(字符序列、int start、int before、int count){
searchString=search.getText().toString();
新建LoadTopics().execute();
}
公共无效后文本已更改(可编辑){
}
};
类LoadTopics扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(TopicsActivity.this);
setMessage(“正在加载主题,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
}
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
添加(新的BasicNameValuePair(“searchString”,searchString));
JSONObject json=jParser.makeHttpRequest(url_主题,“GET”,参数);
Log.d(“所有主题:,json.toString());
试一试{
int success=json.getInt(“success”);
如果(成功==1){
Jtopics=json.getJSONArray(“主题”);
for(int i=0;i
在我的代码中,我有一个TextWatcher。每次我在EditText中键入文本时,我都希望列表视图刷新/更新/清除并重新填充。其目的是在EditText中搜索作为用户权限关键字的主题。原样的代码将检索正确的主题,但对于EditText中输入的每个字母,列表将再次填充相同的主题,因此我将有重复的,然后是重复的,依此类推。我尝试了
listview.setAdapter(null)
,我尝试了使用
adapter.clear()
,在OnTextChanged方法中清除适配器。我还尝试了
topics.clear()
,然后是
adapter.notifyDataSetChanged()
。我知道已经有类似于我的问题,但它们似乎无法解决我的问题。我做错了什么


谢谢

您需要创建一个baseadapter类并实现filterable。然后在该类中创建一个扩展filter的私有筛选器类。并在代码中使用该基本适配器类创建一个适配器并使用adapter.getFilter().filter(newText)在您的on text changed方法中。

您是否尝试了AutoCompleteTextView。这将帮助您实现所需的功能。但即使它提供了建议,我仍然需要检索它们,对吗?因此,如果用户继续键入,它们不会被检索多次?我没有创建baseadapter类。我只是将
listView.setTextFilterE放入nabled(true);
在我的postexecute中,并在我的on text更改方法中使用了
adapter.getFilter().filter(searchString)
,就像您建议的那样,它可以工作。谢谢:)
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);


            }
        });

    }

}