If there was just one value in a row. I got the answer but i have more than one.
This my main class which i couldnt decide how to fill Textwatcher stuff. "CityArray" is the class which i created the rows and "CityXmlParse" is the class that i take data from XML file called "cities.xml" in the raw folder. Each row has an image and a name and i want to filter the rows by name while typing, but whole row must be seen after typing (with image).
public class TravelFinalActivity extends 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
}
});
} }
And here is the class for xml parsing.
public class 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) {}
}
}
Finally this is my "CityArray" constructor and the method for customing row.
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;
}
Thanks in advance!