I have a listview which contains more that 1 value

2019-09-20 15:15发布

问题:

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!

回答1:

  • Making Items in an ArrayAdapter filterable: ArrayAdapter has a built in ArrayFilter , It compares the Objects in adapter list by using a string from toString().toLowerCase() for each object. If you override toString() in CityData class and return city name, then ArrayAdapter should be able to filter items effectively.

  • Enabling Automatic text filter on a ListView: In ListView either use android:textFilterEnabled="true" in layout or set it from code using setTextFilterEnabled(true). Now whenever listView is in focus, user can simply bring up the keyboard, and start typing, list items will be automatically filtered.

  • Explicitly setting text filter on a ListView: Use setFilterText() method of ListView. Do not forget to clear this afterward.

You can reveal more details by examining the relevant Android Sources.