android– 无法在更改EditText值时更新listview

android– 无法在更改EditText值时更新listview,第1张

概述我有一个活动,从包含许多国家/地区的信息(名称,资本,人口,旗帜图像)的数据库中检索数据.主布局有一个EditText,用于输入大写名称作为输入和ListView.在启动活动时,将在ListView中检索并简要显示所有国家/地区.然后在EditText中键入时,我希望ListView显示具有匹配大写名称的国家/地

我有一个活动,从包含许多国家/地区的信息(名称,资本,人口,旗帜图像)的数据库中检索数据.主布局有一个EditText,用于输入大写名称作为输入和ListVIEw.在启动活动时,将在ListVIEw中检索并简要显示所有国家/地区.然后在EditText中键入时,我希望ListVIEw显示具有匹配大写名称的国家/地区.但我看到的是一个空白的ListVIEw.此外,如果EditText为空,我希望ListVIEw显示开头显示的所有国家/地区.

主要布局文件如下:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:ID="@+ID/parentLayout"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="#FFFFFF"    androID:orIEntation="vertical" >    <com.Google.androID.gms.ads.AdVIEw        xmlns:ads="http://schemas.androID.com/apk/res-auto"        androID:ID="@+ID/adVIEw"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_alignParentBottom="true"        ads:adSize="SMART_BANNER"        ads:adUnitID="@string/banner_ad_ID"        androID:visibility="gone" />    <relativeLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_above="@ID/adVIEw"        androID:layout_marginBottom="10dp"        androID:layout_marginleft="10dp"        androID:layout_marginRight="10dp"        androID:layout_margintop="5dp" >        <EditText             androID:ID="@+ID/search_input_cAPItal"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:inputType="text"            androID:layout_alignParenttop="true"           androID:visibility="visible" >        <requestFocus />        </EditText>        <ListVIEw            androID:ID="@+ID/ListVIEw"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:cachecolorHint="#00000000"            androID:divIDer="@androID:color/transparent"            androID:drawSelectorOntop="false"             androID:layout_below="@ID/search_input_cAPItal">        </ListVIEw>    </relativeLayout></relativeLayout>

而活动的代码(CAPItalActivity.java)是:

   protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_cAPItal);        private ArrayList<Country> items;        private ArrayList<Country> items_all;        private ArrayList<Country>  items_new;        private EfficIEntAdapter adapter;        private ListVIEw ListVIEw;        EditText searchinput=(EditText)findVIEwByID(R.ID.search_input_cAPItal);        items = new ArrayList<Country>();        items_new = new ArrayList<Country>();        ListVIEw = (ListVIEw) findVIEwByID(R.ID.ListVIEw);        ListVIEw.setdivIDerHeight(10);        adapter = new EfficIEntAdapter(CAPItalActivity.this);        ListVIEw.setAdapter(adapter);       setListItems("");      searchinput.addTextChangedListener(new TextWatcher() {               @OverrIDe               public voID afterTextChanged(Editable s) {               }               @OverrIDe                   public voID beforeTextChanged(CharSequence s, int start,                 int count, int after) {               }               @OverrIDe                   public voID onTextChanged(CharSequence s, int start,                 int before, int count) {                   String searchCAPItal = new String(s.toString()).trim();                 items_new.clear();               for(Country myCountry  : items_all) {               if( myCountry.getCAPItal() != null &&                       myCountry.getCAPItal().trim().tolowerCase().contains(searchCAPItal)) {               items_new.add(myCountry);                 }                }             items.clear();            items = (ArrayList<Country>) items_new.clone();            if(searchCAPItal.trim().isEmpty()){            items = items_all;            }            adapter.notifyDataSetChanged();               }              });private voID setListItems(String searchKey) {    items_all= DatabaseAccessor.getCountryList(CAPItalActivity.this, type, typeValue, searchKey);    items=items_all;    adapter.notifyDataSetChanged();}

上面只显示了必要的代码段.我该怎么做才能达到目标?

EDIT1:我在上面粘贴的代码中大部分省略了EfficIEntAdapter实现部分.但是这次我也粘贴了这段代码:

public class EfficIEntAdapter extends BaseAdapter {        private LayoutInflater mInflater;        private Context context;        public EfficIEntAdapter(Context context) {            mInflater = LayoutInflater.from(context);            this.context = context;        }        public VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) {            VIEwHolder holder;            if (convertVIEw == null) {                convertVIEw = mInflater.inflate(R.layout.country_container, null);                holder = new VIEwHolder();                holder.nameVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.nameVIEw);                holder.continentVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.continentVIEw);                holder.imageVIEw = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.imageVIEw);                holder.detailsVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.detailsVIEw);                convertVIEw.setTag(holder);            } else {                holder = (VIEwHolder) convertVIEw.getTag();            }            OnClickListener ocl = new OnClickListener() {                @OverrIDe                public voID onClick(VIEw v) {                    Intent intent = new Intent(CAPItalActivity.this, CountryLearningActivity.class);                    intent.putExtra(Constants.KEY_COUNTRY, items);                    intent.putExtra(Constants.KEY_INDEX, position);                    startActivity(intent);                }            };            Country item = items.get(position);            holder.nameVIEw.setText(item.getCountry());            if (type.equalsIgnoreCase(filters[0]) || type.equalsIgnoreCase(filters[1])                    || type.equalsIgnoreCase(filters[2])) {                holder.continentVIEw.setText(item.getContinent());            } else if (type.equalsIgnoreCase(filters[3])) {                holder.continentVIEw.setText(HTML                        .fromHTML("Area: " + formatter.format(Double.parseDouble(item.getArea())) + " km<sup>2</sup>"));            } else if (type.equalsIgnoreCase(filters[4])) {                holder.continentVIEw.setText("Population : " + item.getPopulation());            }            holder.imageVIEw.se@R_404_5411@Resource(Utils.getResID(CAPItalActivity.this, item.getFlag()));            holder.detailsVIEw.setonClickListener(ocl);            convertVIEw.setonClickListener(ocl);            return convertVIEw;        }        class VIEwHolder {            TextVIEw nameVIEw;            ImageVIEw imageVIEw;            TextVIEw continentVIEw;            TextVIEw detailsVIEw;        }        @OverrIDe        public long getItemID(int position) {            return 0;        }        @OverrIDe        public int getCount() {            return items.size();        }        @OverrIDe        public Object getItem(int position) {            return items.get(position);        }    }

解决方法:

只需打开当前页面中的相同页面即可获得以下代码

enter cod  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_cAPItal);    private ArrayList<Country> items;    private ArrayList<Country> items_all;    private ArrayList<Country>  items_new;    private EfficIEntAdapter adapter;    private ListVIEw ListVIEw;    EditText searchinput=(EditText)findVIEwByID(R.ID.search_input_cAPItal);    items = new ArrayList<Country>();    items_new = new ArrayList<Country>();    ListVIEw = (ListVIEw) findVIEwByID(R.ID.ListVIEw);    ListVIEw.setdivIDerHeight(10);    adapter = new EfficIEntAdapter(CAPItalActivity.this);    ListVIEw.setAdapter(adapter);   setListItems("");  searchinput.addTextChangedListener(new TextWatcher() {           @OverrIDe           public voID afterTextChanged(Editable s) {           }           @OverrIDe               public voID beforeTextChanged(CharSequence s, int start,             int count, int after) {           }           @OverrIDe               public voID onTextChanged(CharSequence s, int start,             int before, int count) {               String searchCAPItal = new String(s.toString()).trim();             items_new.clear();           for(Country myCountry  : items_all) {           if( myCountry.getCAPItal() != null &&                   myCountry.getCAPItal().trim().tolowerCase().contains(searchCAPItal)) {           items_new.add(myCountry);             }            }         items.clear();        items = (ArrayList<Country>) items_new.clone();        if(searchCAPItal.trim().isEmpty()){        items = items_all;        }        adapter.notifyDataSetChanged();           }          });

private voID setListItems(String searchKey){

items_all= DatabaseAccessor.getCountryList(CAPItalActivity.this, type, typeValue, searchKey);items=items_all;adapter.notifyDataSetChanged();

在这里

总结

以上是内存溢出为你收集整理的android – 无法在更改EditText值时更新listview全部内容,希望文章能够帮你解决android – 无法在更改EditText值时更新listview所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1099118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存