如何使用CustomAdapter与XML异步任务的ListView [英] How to use CustomAdapter with ListView from XML Async Task

查看:110
本文介绍了如何使用CustomAdapter与XML异步任务的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现填充的ListView 使用的的StringBuilder 从互联网XML源。

通过这个$ C C ListView的$填充只有一个字符串,但我想填充ListView通过元素: getIdLine getTimeLeft (用 CustomAdapter )进行自定义列表视图项的布局中分离出来的字符串。

如何实现这一目标?

EDITED code

FragmentActivity.class

 私人的ListView listViewEMT;
私人的ArrayList< HashMap的<字符串,字符串>> yourList;

... AsyncTask的

 保护无效onPostExecute(字符串字符串){
            super.onPostExecute(串);
CustomAdapter适配器=新CustomAdapter(getActivity(),yourList);
            listViewEMT.setAdapter(适配器);
            this.progressDialog.dismiss();


        }

 / ** RSS处理程序类* /


    类位于RSSHandler扩展的DefaultHandler {

        StringBuffer的字符;
        私人到达currentArrival;

        位于RSSHandler(){
            this.currentArrival = NEW ARRIVAL();
            this.chars =新的StringBuffer();
        }

        公共无效字符(字符[] ARRC,整数N,INT N2){
            this.chars.append(新的String(ARRC,N,N2));
        }

        公共无效的endElement(字符串字符串,字符串字符串2,字符串STRING3)抛出的SAXException {
            super.endElement(字符串,字符串2,STRING3);
            如果((string2.equalsIgnoreCase(idStop))及及(this.currentArrival.getIdStop()== NULL)){
                this.currentArrival.setIdStop(this.chars.toString());
            }
            如果((string2.equalsIgnoreCase(idLine))及及(this.currentArrival.getIdLinea()== NULL)){
                this.currentArrival.setIdLinea(this.chars.toString());
            }
            如果((string2.equalsIgnoreCase(TimeLeftBus))及及(this.currentArrival.getTimeLeft()== 0)){
                INT N = Integer.valueOf((字符串)(this.chars.toString()));
                this.currentArrival.setTimeLeft(N);
            }
            如果收益率((string2.equalsIgnoreCase(到达))!);
            yourList.add((&HashMap中所述;字符串,字符串&GT)(currentArrival.getMap()));
            this.currentArrival = NEW ARRIVAL();
        }

        公共无效的startElement(字符串字符串,字符串字符串2,字符串STRING3,org.xml.sax.Attributes属性)抛出的SAXException {
            super.startElement(字符串,字符串2,STRING3,属性);
            this.chars =新的StringBuffer();
            string2.equalsIgnoreCase(到达);
        }

    }
 

Arrival.class

  ... getter和setter
 公众的HashMap<字符串,字符串>的GetMap(){

        HashMap的<字符串,字符串>地图;
        图=新的HashMap<字符串,字符串>();
        //添加每个子节点HashMap中的key =>值
        map.put(KEY1,idLinea);
        map.put(KEY2,将String.valueOf(的timeleft));



        返回地图;

    }
 

CustomAdapter.class 感谢Nabin

 公共类CustomAdapter扩展了BaseAdapter {
    私人活动的活动;
    私人的ArrayList< HashMap的<字符串,字符串>>数据;
    私有静态LayoutInflater吹气= NULL;
    私人列表<字符串> listString;

    公共CustomAdapter(活动一,ArrayList的< HashMap的<字符串,字符串>> D){
        活性= A;
        数据= D;
        充气=(LayoutInflater)活动
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @覆盖
    公众诠释getCount将(){
        返回0;
    }

    @覆盖
    公共对象的getItem(INT位置){
        返回data.get(位置);
    }

    @覆盖
    众长getItemId(INT位置){
        返回0;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看VI = convertView;
        如果(convertView == NULL)
            VI = inflater.inflate(R.layout.emt_item,NULL);

        TextView的TV1 =(TextView中)vi.findViewById(R.id.itemLine);
        TextView的TV2 =(TextView中)vi.findViewById(R.id.itemTime);
        HashMap的<字符串,字符串>地图;
        地图= data.get(位置);
        tv1.setText(map.get(KEY1));
        tv2.setText(map.get(KEY2));
        返回六;
    }

}
 

解决方案

创建一个自定义适配器如下:

 公共类ArrayAdapter扩展了BaseAdapter {
私人活动的活动;
私人的ArrayList< HashMap的<字符串,字符串>>数据;
私有静态LayoutInflater吹气= NULL;
私人列表<字符串> listString;

公共ArrayAdapter(活动一,ArrayList的< HashMap的<字符串,字符串>> D){
    活性= A;
    数据= D;
    充气=(LayoutInflater)活动
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//你的getView此方法
}
 

GetView方法

 公开查看getView(INT位置,查看convertView,ViewGroup中父){
        查看VI = convertView;
        如果(convertView == NULL)
            VI = inflater.inflate(R.layout.custom,NULL);

        TextView的TV1 =(TextView中)vi.findViewById(R.id.tvone);
        TextView的TV2 =(TextView中)vi.findViewById(R.id.tvtwo);
        HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
        地图= data.get(位置);
        tv1.setText(map.get(KEY1));
        tv2.setText(map.get(KEY2));
        返回六;
    }
 

请数组列表为:

 的ArrayList< HashMap的<字符串,字符串>> yourList;
 

和填写 yourList 作为

 的HashMap<字符串,字符串>图=新的HashMap<字符串,字符串>();
//添加每个子节点HashMap中的key =>值
map.put(KEY1,值1);
map.put(KEY2,值2);
yourList.add(图)
 

和同时使定制适配器对象

  CustomAdapter适配器=新CustomAdapter(YourActivity.this,yourList);
list.setAdapter(适配器);
 

有关列表你可以做

 名单=(ListView控件)getView()findViewById(android.R.id.list)。
 

I achieved populate a ListView using StringBuilder from a Internet XML Source.

With this code the listView is populated with only one String but I want populate the listview by elements: getIdLine and getTimeLeft (With CustomAdapter) for customize the layout of the listView items in separated Strings.

How to achieve this?

EDITED CODE

FragmentActivity.class

private ListView listViewEMT;
private ArrayList<HashMap<String, String>> yourList;

... AsyncTask

 protected void onPostExecute(String string) {
            super.onPostExecute(string);
CustomAdapter adapter = new CustomAdapter(getActivity(), yourList);
            listViewEMT.setAdapter(adapter);
            this.progressDialog.dismiss();


        }

 /** RSS HANDLER CLASS */


    class RSSHandler extends DefaultHandler {

        StringBuffer chars;
        private Arrival currentArrival;

        RSSHandler() {
            this.currentArrival = new Arrival();
            this.chars = new StringBuffer();
        }

        public void characters(char[] arrc, int n, int n2) {
            this.chars.append(new String(arrc, n, n2));
        }

        public void endElement(String string, String string2, String string3) throws SAXException {
            super.endElement(string, string2, string3);
            if ((string2.equalsIgnoreCase("idStop")) && (this.currentArrival.getIdStop() == null)) {
                this.currentArrival.setIdStop(this.chars.toString());
            }
            if ((string2.equalsIgnoreCase("idLine")) && (this.currentArrival.getIdLinea() == null)) {
                this.currentArrival.setIdLinea(this.chars.toString());
            }
            if ((string2.equalsIgnoreCase("TimeLeftBus")) && (this.currentArrival.getTimeLeft() == 0)) {
                int n = Integer.valueOf((String)(this.chars.toString()));
                this.currentArrival.setTimeLeft(n);
            }
            if (!(string2.equalsIgnoreCase("Arrive"))) return;
            yourList.add((HashMap<String, String>)(currentArrival.getMap()));
            this.currentArrival = new Arrival();
        }

        public void startElement(String string, String string2, String string3, org.xml.sax.Attributes attributes) throws SAXException {
            super.startElement(string, string2, string3, attributes);
            this.chars = new StringBuffer();
            string2.equalsIgnoreCase("Arrive");
        }

    }

Arrival.class

...getters and setters
 public HashMap<String, String> getMap() {

        HashMap<String, String> map;
        map = new HashMap<String, String>();
        // adding each child node to HashMap key => value
        map.put("KEY1", idLinea);
        map.put("KEY2", String.valueOf(timeLeft));



        return map;

    }

CustomAdapter.class Thanks to Nabin

public class CustomAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater = null;
    private List<String> listString;

    public CustomAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.emt_item, null);

        TextView tv1 = (TextView) vi.findViewById(R.id.itemLine);
        TextView tv2 = (TextView) vi.findViewById(R.id.itemTime);
        HashMap<String, String> map;
        map = data.get(position);
        tv1.setText(map.get("KEY1"));
        tv2.setText(map.get("KEY2"));
        return vi;
    }

}

解决方案

Create a custom adapter as following:

public class ArrayAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
private List<String> listString;

public ArrayAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//your getView method here
}

GetView Method

public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.custom, null);

        TextView tv1 = (TextView) vi.findViewById(R.id.tvone);
        TextView tv2 = (TextView) vi.findViewById(R.id.tvtwo);
        HashMap<String, String> map = new HashMap<String, String>();
        map = data.get(position);
        tv1.setText(map.get("KEY1"));
        tv2.setText(map.get("KEY2"));
        return vi;
    }

Make array list as:

ArrayList<HashMap<String, String>> yourList;

And fill yourList as

HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("KEY1", value1);
map.put("KEY2", value2);
yourList.add(map);

And while making object of the custom adapter

CustomAdapter adapter = new CustomAdapter(YourActivity.this, yourList);
list.setAdapter(adapter);

For list you can do

list = (ListView) getView().findViewById(android.R.id.list);

这篇关于如何使用CustomAdapter与XML异步任务的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆