解析Xml,在ListView中显示它并添加搜索框

时间:2011-05-09 21:55:44

标签: java android xml listview search-box

我在这里......再次遇到很多问题。

首先,我的应用正在解析本地xml。

public class CountiesHandler {

    final int stateUnknown      = 0;
    final int stateContinente   = 1;
    final int statePais         = 2;
    final int stateOperador     = 3;
    final int stateFrecuencia   = 4;
    final int stateFirma        = 5;
    final int stateGsm          = 6;
    final int stateGprs         = 7;
    final int stateT3g          = 8;
    final int statePrepago      = 9;
    final int stateSms          = 10;
    final int stateZona         = 11;
    final int stateEstatus      = 12;

    int currentState = stateUnknown;

    int type;

    int cm = R.xml.coberturamundial;

    Counties counties;
    County county;

    public CountiesHandler (int _type) {
        counties = new Counties();
        type = _type;
    }

    public Counties getCounties(){
        return counties;
    }

    public void parseCounties(Activity activity) throws XmlPullParserException, IOException {

        Resources resource = activity.getResources();

        XmlResourceParser xrp = resource.getXml(cm);

        xrp.next();
        int eventType = xrp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT){

            if(eventType == XmlPullParser.START_DOCUMENT){
                //START DOCUMENT                
            }else if(eventType == XmlPullParser.START_TAG){             

                //START TAG
                String element = xrp.getName();
                if(element.equalsIgnoreCase("county")){
                    county = new County();
                }else if(element.equalsIgnoreCase("continente")){
                    currentState = stateContinente;
                }else if(element.equalsIgnoreCase("pais")){
                    currentState = statePais;
                }else if(element.equalsIgnoreCase("operador")){
                    currentState = stateOperador;
                }else if(element.equalsIgnoreCase("frecuencia")){
                    currentState = stateFrecuencia;
                }else if(element.equalsIgnoreCase("firma")){
                    currentState = stateFirma;
                }else if(element.equalsIgnoreCase("gsm")){
                    currentState = stateGsm;
                }else if(element.equalsIgnoreCase("gprs")){
                    currentState = stateGprs;
                }else if(element.equalsIgnoreCase("t3g")){
                    currentState = stateT3g;
                }else if(element.equalsIgnoreCase("prepago")){
                    currentState = statePrepago;
                }else if(element.equalsIgnoreCase("sms")){
                    currentState = stateSms;
                }else if(element.equalsIgnoreCase("zona")){
                    currentState = stateZona;
                }else if(element.equalsIgnoreCase("estatus")){
                    currentState = stateEstatus;
                }               


            }else if(eventType == XmlPullParser.END_TAG){
                //END TAG  
                if(xrp.getName().equalsIgnoreCase("county")){
                    endElement();
                }
            }else if(eventType == XmlPullParser.TEXT){
                //TEXT              
                String text = xrp.getText();
                setElement(text);               
            }else{
                currentState = stateUnknown;
            }           
            eventType = xrp.next();
        }
    }

    void setElement(String text){
        switch(currentState){           
            case stateContinente:           
                county.setContinente(text);         
                break;
            case statePais:         
                county.setPais(text);
                break;
            case stateOperador:         
                county.setOperador(text);
                break;
            case stateFrecuencia:           
                county.setFrecuencia(text);
                break;
            case stateFirma:            
                county.setFirma(text);
                break;
            case stateGsm:
                county.setGsm(text);
                break;
            case stateGprs: 
                county.setGprs(text);
                break;
            case stateT3g:
                county.setT3g(text);
                break;
            case statePrepago:
                county.setPrepago(text);
                break;
            case stateSms:
                county.setSms(text);
                break;
            case stateZona:
                county.setZona(text);
                break;
            case stateEstatus:
                county.setEstatus(text);
                break;
        }
    }

    void endElement() {
        counties.add(county);
    }

}

然后我

public class CountiesAdapter {
    private Activity activity;
    private List<County> data;
    private static LayoutInflater inflater=null;

    public CountiesAdapter(Activity a, List<County> d) {        
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
    }

    public int getCount() {
        return data.size();        
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder{

        public TextView text;   
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        ViewHolder holder;

        if(convertView==null){          
            holder=new ViewHolder();            
            vi = inflater.inflate(R.layout.countylistitem, null);
            holder.text = (TextView)vi.findViewById(R.id.countylistleyend);    
            vi.setTag(holder);
        }else        
            holder=(ViewHolder)vi.getTag();

        String txt = data.get(position).getPais(); 
        holder.text.setText(txt);

       return vi;
    }
}

我正在寻找一个搜索框,显然是在listview中搜索

public class SelectAdonde extends Activity {

    private EditText ed;

    int textlength=0;


    @Override
    public void onCreate(Bundle icicle)
    {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      

        super.onCreate(icicle);

        setContentView(R.layout.countylist);


        ed=(EditText)findViewById(R.id.search_box);
        ed.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }


            public void onTextChanged(CharSequence s, int start, int before, int count) {

                textlength=ed.getText().length();


            }
        });


;
}

}

但我不知道如何整合listview,所有的例子都是使用ArrayAdapter,我是Java的初学者,甚至是Android的初学者。

有人可以帮助我吗?请!感谢。

1 个答案:

答案 0 :(得分:0)

在将其放入Listview之前将其存储在数据库中。然后使用游标适配器从数据库中检索数据,并将游标适配器绑定到列表视图。