How to use CustomAdapter with ListView from XML Async Task

Deadly 提交于 2019-12-08 03:30:54

问题


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

}

回答1:


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


来源:https://stackoverflow.com/questions/25457812/how-to-use-customadapter-with-listview-from-xml-async-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!