Java 使用自定义适配器将数据从XML解析到列表中

Java 使用自定义适配器将数据从XML解析到列表中,java,android,arrays,listview,xml-parsing,Java,Android,Arrays,Listview,Xml Parsing,我从上传到网站的XML中获取数据。我想在自定义的ListView中显示XML中的文本,它有两个TextViews。“标题”应该放在上部TextView中,“guid”应该放在下部TextView中。我不知道该怎么做。我已经编写了以下自定义适配器 CustomAdapter.java public class CustomAdapter extends BaseAdapter { private ArrayList list; private LayoutInflater lay

我从上传到网站的XML中获取数据。我想在自定义的
ListView
中显示XML中的文本,它有两个
TextView
s。“标题”应该放在上部
TextView
中,“guid”应该放在下部
TextView
中。我不知道该怎么做。我已经编写了以下自定义适配器

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

    private ArrayList list;
    private LayoutInflater layoutInflater;

    public CustomAdapter(Context context, ArrayList list) {
        this.list= list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {

            convertView = layoutInflater.inflate(R.layout.layout_list_row, null);

            holder = new ViewHolder();
            holder.backgroundImage = (ImageView) convertView.findViewById(R.id.backgroundImage);
            holder.topText = (TextView) convertView.findViewById(R.id.topText);
            holder.bottomText = (TextView) convertView.findViewById(R.id.bottomText);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        DiscourseItem discourseItem = (DiscourseItem) discourseList.get(position);
        holder.topText.setText(discourseItem.getTopText());
        holder.bottomText.setText(discourseItem.getBottomText());

        return convertView;
    }

    static class ViewHolder {
        ImageView backgroundImage;
        TextView topText;  //This should display the text in the 'title' field
        TextView bottomText; //This should display the text in the 'guid' field
    }

}
目前,我可以使用普通的
ArrayAdapter
s在单独的
ListView
s中分别显示这两个。这是我写的代码

XMLParser.java

public class XMLParser extends AsyncTask {

    private URL url;
    public ArrayList<String> title = new ArrayList<>();
    public ArrayList<String> guid = new ArrayList<>();

    @Override
    protected Object doInBackground(Object[] objects) {
        try {
            url = new URL(removed);

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(getInputStream(url), null);

            boolean insideItem = false;

            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if (xpp.getName().equalsIgnoreCase("item")){
                        insideItem = true;
                    } else if (xpp.getName().equalsIgnoreCase("title")){
                        if (insideItem)
                            title.add(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("guid")){
                        if (insideItem)
                            guid.add(xpp.nextText());
                    }
                } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                    insideItem = false;
                }
                eventType = xpp.next();
            }
        } catch (MalformedURLException e){
            e.printStackTrace();
        } catch (XmlPullParserException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return title;
    }

    public InputStream getInputStream(URL url){
        try {
            return url.openConnection().getInputStream();
        } catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

    public ArrayList<String> titles(){
        return title;
    }

    public ArrayList<String> guids(){
        return guid;
    }
}

您可以为您的上下值创建包装类(类似于此,考虑另一个名称,因为我不知道数据的上下文):

然后将结果解析到TitleGuidPair的ArrayList。如果你想保留你的解析算法,你可以做一些后处理,然后从你拥有的两个列表中建立你的标题指南

在这一步之后,只需传递到TitleGuidPairs的适配器列表,并在getView方法中设置顶部和底部文本,如

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {

        convertView = layoutInflater.inflate(R.layout.layout_list_row, null);

        holder = new ViewHolder();
        holder.backgroundImage = (ImageView) convertView.findViewById(R.id.backgroundImage);
        holder.topText = (TextView) convertView.findViewById(R.id.topText);
        holder.bottomText = (TextView)    convertView.findViewById(R.id.bottomText);
        TitleGuidPair titleGuidPair = list.get(position);  
        holder.topText.setText(titleGuidPair.getTitle());
        holder.bottomText.setText(titleGuidPair.getGuid());
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}

因此,我做了一些更改,使用一个
列表视图来显示数据,但它抛出了一个
ArrayIndexOutOfBoundsException
。我已更新OP以反映代码和错误。您声明数组的大小为title.size()。因此,您可以访问从0到大小为-1的索引。在循环中,您在从0到大小的索引中闪烁。请将您的循环从(int i=0;i是的,非常感谢。在我发布此内容后,我再次查看了我的代码并注意到了这一点。是的,每个标题都有一个guid,因此这不会成为问题。非常感谢!
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
public class TitleGuidPair {
  private final String title;
  private final String guid;
  public TitleGuidPair(String title, String guid) {
    this.title = title;
    this.guid = guid;
  }
 //getters
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {

        convertView = layoutInflater.inflate(R.layout.layout_list_row, null);

        holder = new ViewHolder();
        holder.backgroundImage = (ImageView) convertView.findViewById(R.id.backgroundImage);
        holder.topText = (TextView) convertView.findViewById(R.id.topText);
        holder.bottomText = (TextView)    convertView.findViewById(R.id.bottomText);
        TitleGuidPair titleGuidPair = list.get(position);  
        holder.topText.setText(titleGuidPair.getTitle());
        holder.bottomText.setText(titleGuidPair.getGuid());
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}