如果一行中只有一个值。我得到了答案,但我有不止一个。
这是我的主要课程,我无法决定如何填充 Textwatcher 的东西。“CityArray”是我创建行的类,“CityXmlParse”是我从原始文件夹中名为“cities.xml”的 XML 文件获取数据的类。每行都有一个图像和一个名称,我想在输入时按名称过滤行,但输入后必须看到整行(带图像)。
公共类 TravelFinalActivity 扩展 Activity {
EditText sc;
ListView lv;
List<CityData> citylist;
CityArray adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sc = (EditText) findViewById(R.id.citySearch);
CityXmlParse cityParse = new CityXmlParse();
InputStream in = getResources().openRawResource(R.raw.cities);
cityParse.xmlParse(in);
citylist = cityParse.getList();
adapter = new CityArray(getApplicationContext(),R.layout.city_row, citylist);
lv = (ListView) this.findViewById(R.id.cityList);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
sc.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
} }
这是用于 xml 解析的类。
公共类 CityXmlParse{
private final List<CityData> list = new ArrayList<CityData>();
private String getNodeValue(NamedNodeMap map, String key) {
String nodeValue = null;
Node node = map.getNamedItem(key);
if (node != null) {
nodeValue = node.getNodeValue();
}
return nodeValue;
}
public List<CityData> getList(){
return this.list;
}
public void xmlParse(InputStream in){
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList cityList = doc.getElementsByTagName("city");
for(int i=0; i<cityList.getLength(); i++){
final NamedNodeMap cityAttr = cityList.item(i).getAttributes();
final String cityName=getNodeValue(cityAttr, "name");
final String cityInfo=getNodeValue(cityAttr, "info");
CityData cityObj = new CityData(cityName, cityInfo, cityName + ".png");
list.add(cityObj);
}
}catch (Throwable T) {}
}
}
最后这是我的“CityArray”构造函数和自定义行的方法。
public CityArray(Context context, int textViewResourceId, List<CityData> citylist) {
super(context, textViewResourceId, citylist);
this.citylist=citylist;
this.context=context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null){
inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.city_row, parent,false);
}
CityData cityObj = getItem(position);
cityImage = (ImageView) rowView.findViewById(R.id.city_image);
cityName = (TextView) rowView.findViewById(R.id.city_name);
String imgPath = ASSETS_DIR + cityObj.resourceImg;
try {
Bitmap bitmap = BitmapFactory.decodeStream(this.context.getResources().getAssets().open(imgPath));
cityImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
cityName.setText(cityObj.name);
return rowView;
}
提前致谢!