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

发布于 2024-11-05 21:04:50 字数 7134 浏览 3 评论 0原文

我又来了,有很多问题。

首先,我的应用程序正在解析本地 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;
    }
}

并且我正在尝试在列表视图中显示一个搜索框,显然是搜索

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();


            }
        });


;
}

}

但我不知道如何集成列表视图,所有示例都是使用 ArrayAdapter 的,我是 Java 的初学者,甚至更多关于 Android 的信息。

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

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 someone help me? Please! Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2024-11-12 21:04:50

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文