开发者

Parsing Xml, displaying it in a ListView and adding a Search Box

开发者 https://www.devze.com 2023-03-04 06:41 出处:网络
I\'m here... again, having a lot of problems. First, my app is parsing a local xml. public class CountiesHandler {

I'm here... again, having a lot of problems.

First, my app is parsing a local 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);
    }

}

Then I have

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;
    }
}

and I'm traying to show a search box, obviously that search, within the 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();


            }
        });


;
}

}

But I dont know how to integrate the listview, all the examples are with ArrayAdapter and I'm a beginner with Java and even more with Android.

Could some开发者_开发知识库one help me? Please! Thanks.


Store it in a database before putting it in Listview. Then retrieve the data from the database using a cursor adapter and bind the cursor adapter to the listview.

0

精彩评论

暂无评论...
验证码 换一张
取 消