Autocompletextview显示previous值 [英] Autocompletextview shows previous values

查看:165
本文介绍了Autocompletextview显示previous值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时autocompletetextview。我试过notifydatasetchanged()显示,从ArrayList中删除previous数据,但它被设置适配器之前。我试图在所有。我想我需要的地方添加它来实现添加filter.But我不知道该怎么办它。我真的AP preciate任何help.Thanks提前。

BaseAdapter

 公共类SimpleAdapter延伸BaseAdapter {    私人活动活动;
    私人的ArrayList<&HashMap的LT;字符串,字符串>>数据;
    私有静态LayoutInflater吹气= NULL;
    公共ImageLoader的ImageLoader的;
    位图图像;
    私人DisplayImageOptions defaultOptions;    公共SimpleAdapter(活动一,ArrayList的<&HashMap的LT;字符串,字符串> D 1和D){
        活性=一个;
        数据= D;
        吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        defaultOptions =新DisplayImageOptions.Builder()建立()。        ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(活动)
        .threadPoolSize(2)
        .memoryCache(新WeakMemoryCache())
        .discCacheFileNameGenerator(新Md5FileNameGenerator())
        。建立();        ImageLoader的= ImageLoader.getInstance();    imageLoader.init(配置);        imageLoader.init(ImageLoaderConfiguration.createDefault(活动));
    }    公众诠释的getCount(){
        返回data.size();
    }    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        查看VI = convertView;
        如果(convertView == NULL)
            VI = inflater.inflate(R.layout.list_row,NULL);        TextView的标题=(TextView中)vi.findViewById(R.id.textView1); //称号
        TextView的艺术家=(TextView中)vi.findViewById(R.id.textView2); //歌手名
        TextView的持续时间=(TextView中)vi.findViewById(R.id.textView3); //持续时间
        ImageView的thumb_image =(ImageView的)vi.findViewById(R.id.list_image); //拇指图像        HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();
        曲= data.get(位置);        //设置列表视图中的所有值
        title.setText(song.get(名字));
        artist.setText(song.get(姓氏));
        duration.setText(song.get(时代));
        imageLoader.displayImage(song.get(链接),thumb_image,defaultOptions);        返回VI;
    }
}

MainActivity

 公共类AutoCompleteTextViewActivity扩展活动实现TextWatcher {
    / **当第一次创建活动调用。 * /
    私人TextView的选择;
    私人AutoCompleteTextView ACTV;
    SimpleAdapter适配器;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
       选择=(的TextView)findViewById(R.id.selection);
       ACTV =(AutoCompleteTextView)findViewById(R.id.actv);
       actv.addTextChangedListener(本);    }    公共无效afterTextChanged(编辑S){
        // TODO自动生成方法存根    }    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
            INT后){
        // TODO自动生成方法存根    }    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){        建议Suggestdetails =新建议();
        Suggestdetails.execute();
    }    公共类推荐扩展的AsyncTask<无效,无效的JSONObject> {
        @覆盖
        受保护的JSONObject doInBackground(虚空...... PARAMS){             字符串ResponseBody = NULL;            尝试{
                // geoaddress这里                HttpClient的HttpClient的=新DefaultHttpClient();
                HttpPost httppost =新HttpPost(http://example.com/suggest.php);                //添加数据
                清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(1);                nameValuePairs.add(新BasicNameValuePair(名,actv.getText()的toString()));
                httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));                //执行HTTP POST请求
                HTT presponse响应= httpclient.execute(httppost);                读者的BufferedReader =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),ISO-8859-1),8);
                StringBuilder的SB =新的StringBuilder();
                sb.append(reader.readLine()+\\ n);
                串行=0;
                而((行= reader.readLine())!= NULL){
                    sb.append(行+\\ n);
                }
                reader.close();
                字符串结果= sb.toString();                Log.d(FB TR,结果);
                //数据解析
                返回新的JSONObject(结果);
            }赶上(例外五){
                e.printStackTrace();
                返回null;
            }
        }        @覆盖
        保护无效onPostExecute(JSONObject的结果){
            如果(结果!= NULL){
                // 做一点事
// JSONObject的TR =结果;        清单<&HashMap的LT;字符串,字符串>> ALIST =新的ArrayList<&HashMap的LT;字符串,字符串>>();            尝试{
              JSONArray ARR = result.getJSONArray(阵列);
            的for(int i = 0; I< arr.length();我++)
            {
                的JSONObject E1 = arr.getJSONObject(ⅰ);                JSONObject的JSON =(JSONObject的)e1.get(数据);                字符串名字= json.getString(名字)修剪()。
                。字符串姓氏= json.getString(姓氏)修剪();
                字符串link = json.getString(链接),TRIM()。
                字符串时间= json.getString(时间)修剪()。
                 HashMap的<字符串,字符串> HM =新的HashMap<字符串,字符串>();
                    hm.put(名字,名字);
                    hm.put(姓氏,姓氏);
                    hm.put(链接的链接);
                    hm.put(时间,时间);
                    aList.add(HM);                }
            }赶上(例外五){
                e.printStackTrace();            }
            的String [] =从{名字,姓氏,时间,链接};
            INT []为= {R.id.textView1,R.id.textView2,R.id.textView3,R.id.list_image};             适配器=新SimpleAdapter(AutoCompleteTextViewActivity.this,ALIST,R.layout.list_row,从,到);               actv.setAdapter(适配器);               adapter.notifyDataSetChanged();
            }其他{
                //出错
            }
        }
    }}


解决方案

执行后您的AsyncTask清除您的ArrayList

 推荐Suggestdetails =新建议();
 如果(ALIST!= NULL){ Suggestdetails.execute();
 aList.clear();
 }其他{ listView.setAdapter(适配器);
 adapter.notifyDataSetChanged(); }

I would like to remove previous data from arraylist while displaying on autocompletetextview .I tried notifydatasetchanged() but it has to be implemented before the adapter is set .I tried adding it in all the place .I guess I need to add filter.But I don't know how to do it.I really appreciate any help.Thanks in Advance.

BaseAdapter

public class SimpleAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader ;
    Bitmap image;
    private DisplayImageOptions defaultOptions;

    public SimpleAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        defaultOptions = new DisplayImageOptions.Builder().build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity)
        .threadPoolSize(2) 
        .memoryCache(new WeakMemoryCache())
        .discCacheFileNameGenerator(new Md5FileNameGenerator())
        .build();

        imageLoader  = ImageLoader.getInstance();

    imageLoader.init(config);

        imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
    }

    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.list_row, null);

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

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

        // Setting all values in listview
        title.setText(song.get("firstname"));
        artist.setText(song.get("lastname")); 
        duration.setText(song.get("time"));
        imageLoader.displayImage(song.get("link"), thumb_image, defaultOptions);

        return vi;
    }


}

MainActivity

public class AutoCompleteTextViewActivity extends Activity implements TextWatcher{
    /** Called when the activity is first created. */
    private TextView selection;
    private AutoCompleteTextView actv;


    SimpleAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       selection=(TextView)findViewById(R.id.selection);
       actv=(AutoCompleteTextView)findViewById(R.id.actv);
       actv.addTextChangedListener(this);



    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

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

        Suggest Suggestdetails=new Suggest();
        Suggestdetails.execute(); 


    }

    public class Suggest extends AsyncTask<Void, Void, JSONObject> {
        @Override
        protected JSONObject doInBackground(Void... params) {

             String ResponseBody = null;

            try {


                //geoaddress here 

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://example.com/suggest.php");

                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

                nameValuePairs.add(new BasicNameValuePair("name", actv.getText().toString()));


                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");
                String line = "0";
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                reader.close();
                String result = sb.toString();

                Log.d("Fb tr",result);
                // parsing data
                return new JSONObject(result);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }



        @Override
        protected void onPostExecute(JSONObject result) {




            if (result != null) {
                // do something
//              JSONObject tr = result;

        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

            try{
              JSONArray arr=result.getJSONArray("Array");


            for(int i=0;i<arr.length();i++)
            {
                JSONObject e1 = arr.getJSONObject(i);

                JSONObject json= (JSONObject) e1.get("data");

                String firstname = json.getString("firstname").trim();  
                String lastname = json.getString("lastname").trim();
                String link = json.getString("link").trim();
                String time = json.getString("time").trim();


                 HashMap<String, String> hm = new HashMap<String,String>();
                    hm.put("firstname", firstname);
                    hm.put("lastname", lastname );
                    hm.put("link", link);
                    hm.put("time", time);
                    aList.add(hm);

                }


            }catch(Exception e){
                e.printStackTrace();

            }


            String[] from = { "firstname","lastname","time","link"};


            int[] to = { R.id.textView1,R.id.textView2,R.id.textView3,R.id.list_image};

             adapter = new SimpleAdapter(AutoCompleteTextViewActivity.this,aList, R.layout.list_row,from,to);

               actv.setAdapter(adapter);

               adapter.notifyDataSetChanged();


            } else {
                // error occured
            }
        }


    }

}

解决方案

After execute your AsyncTask clear your ArrayList.

 Suggest Suggestdetails=new Suggest();
 if(aList!=null){

 Suggestdetails.execute(); 
 aList.clear();
 } else{

 listView.setAdapter(adapter);
 adapter.notifyDataSetChanged(); 

 }

这篇关于Autocompletextview显示previous值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆