首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从XML异步任务中使用CustomAdapter与ListView

如何从XML异步任务中使用CustomAdapter与ListView
EN

Stack Overflow用户
提问于 2014-08-23 02:10:24
回答 1查看 564关注 0票数 1

我实现了使用来自Internet的ListViewStringBuilder进行填充。

使用这段代码,listView只填充一个字符串,但我希望按元素填充列表视图:、getIdLine、getTimeLeft (使用自定义分隔字符串中listview项的布局)。

如何做到这一点?

编辑代码

FragmentActivity.class

代码语言:javascript
复制
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

代码语言:javascript
复制
...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多亏了纳宾

代码语言:javascript
复制
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;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-23 14:43:55

创建自定义适配器,如下所示:

代码语言:javascript
复制
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方法

代码语言:javascript
复制
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;
    }

将数组列表设置为:

代码语言:javascript
复制
ArrayList<HashMap<String, String>> yourList;

并填充yourList作为

代码语言:javascript
复制
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);

以及在创建自定义适配器的对象时

代码语言:javascript
复制
CustomAdapter adapter = new CustomAdapter(YourActivity.this, yourList);
list.setAdapter(adapter);

对于列表,您可以这样做

代码语言:javascript
复制
list = (ListView) getView().findViewById(android.R.id.list);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25457812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档