android - 在更改 EditText 值时无法更新列表视图
Posted
技术标签:
【中文标题】android - 在更改 EditText 值时无法更新列表视图【英文标题】:android - cannot update listview on changing an EditText value 【发布时间】:2016-08-26 09:29:33 【问题描述】:我有一项活动,它从包含许多国家/地区的信息(名称、首都、人口、国旗图像)的数据库中检索数据。主布局有一个以大写名称作为输入的 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_
android:layout_
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_
android:layout_
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_id"
android:visibility="gone" />
<RelativeLayout
android:layout_
android:layout_
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_
android:layout_
android:inputType="text"
android:layout_alignParentTop="true"
android:visibility="visible" >
<requestFocus />
</EditText>
<ListView
android:id="@+id/listView"
android:layout_
android:layout_
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.setImageResource(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);
【问题讨论】:
把CapitalActivity的完整代码贴出来会更好理解。 @Masum,EDIT1
已添加
【参考方案1】:
只需从当前页面打开同一页面即可查看以下代码
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 值时无法更新列表视图的主要内容,如果未能解决你的问题,请参考以下文章
如何在 android 中动态更改 EditText 小部件的文本大小?
如何在 android 的 EditText 上显示数字键盘?