0

我想通过 SAX Parser 解析它并通过 url 加载图像并将其传递给下一个要显示的活动。但是,如果有任何人可以帮助我或给我一些可重用的代码,我将不胜感激。这是我的 xml 文件。这是我解析的数据类和适配器类。现在我想解析数据,如(日期、成本、描述、图像)并将所有内容放入字符串到它们各自的标签中,并在需要时将其传递给下一个活动。请帮助我还需要在图像上实现共享,所以我需要来自 xml 的图像的 url

解析数据类

公共类 ParseData 扩展 DefaultHandler { boolean title , cost , pubDate , link = false;

StringBuffer stringTitle , stringCost  , stringPubdate , stringLink = null;

ArrayList<String> arrtitle = new ArrayList<String>();

ArrayList<String> arrcost = new ArrayList<String>();

ArrayList<String> arrpubDate = new ArrayList<String>();

ArrayList<String> arrlink = new ArrayList<String>();



//---------START ELEMENT----------//

@Override
public void startElement(String uri , String localName , String qName , Attributes attributes) throws SAXException 
{
    super.startElement(uri, localName, qName, attributes);

    if(localName.equalsIgnoreCase("title"))
    {
        title = true;

    }else if(localName.equalsIgnoreCase("cost"))
    {
        cost = true;

    }else if(localName.equalsIgnoreCase("pubDate"))
    {
        pubDate = true;

    }else if(localName.equalsIgnoreCase("link"))
    {
        link = true;
    }
}
//--------END ELEMENT----------//

@Override
public void endElement(String uri , String localName , String qName) throws SAXException 
{
    super.endElement(uri, localName, qName);
    if(localName.equalsIgnoreCase("title"))
    {
        title = false;

    }else if(localName.equalsIgnoreCase("cost"))
    {
        cost = false;

    }else if(localName.equalsIgnoreCase("pubDate"))
    {
        pubDate = false;

    }else if(localName.equalsIgnoreCase("link"))
        link = false;
}

@Override
public void characters(char[] ch, int start, int length)throws SAXException 
{
    super.characters(ch, start, length);


    if(title)
    {
        stringTitle = new StringBuffer("");
    title_flag = true;
    }

    if(cost)
    {
        stringCost = new StringBuffer("");
        cost_flag = true;
    }

    if(pubDate)
    {
       stringPubdate = new StringBuffer("link");
   pubDate_flag = true;
    }

    if(link)
    {
       stringLink = new StringBuffer("");
       link_flag = true;
    }
}

}

连接器类

公共类 BindDataAdapter 扩展 BaseAdapter {

ArrayList<String> title;

ArrayList<String> cost;

ArrayList<String> pubDate;

LayoutInflater inflater;



//---------DEFAULT CONSTRUCTOR-----------//

public BindDataAdapter()
{

}


//-------PARAMETERIZED CONSTRUCTOR------//

public BindDataAdapter(Activity activity , ArrayList<String> title , ArrayList<String> cost , ArrayList<String> pubDate)
{
    this.title = title;

    this.cost = cost;

    this.pubDate = pubDate;

    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}


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

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

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

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

    if(convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_row , null);
    }

    TextView textViewtitle = (TextView) convertView.findViewById(R.id.type);

    TextView textViewcost = (TextView) convertView.findViewById(R.id.cost);

    TextView textViewdate = (TextView) convertView.findViewById(R.id.date);


    //----I coudnot put text view using setText----//


    return convertView;
}

}

这是我的 MainActivity 类

公共类 MainActivity 扩展 Activity {

static final String URLxml = "http://78.46.34.27/kapapps/transaction.xml";

String line = null;



@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    ListView listView = (ListView) findViewById(R.id.listview);


    try 
    {

        SAXParserFactory factory = SAXParserFactory.newInstance();

        SAXParser saxParser = factory.newSAXParser();

        XMLReader reader = saxParser.getXMLReader();

        URL sourceUrl = new URL(URLxml);

        ParseData data = new ParseData();

        reader.setContentHandler(data);

        reader.parse(new InputSource(sourceUrl.openStream()));

        BindDataAdapter bindDataAdapter = new BindDataAdapter(this , data.title , data.cost , data.pubDate);

        listView.setAdapter(bindDataAdapter);



    } catch (ParserConfigurationException e) 
    {
        e.printStackTrace();

    } catch (SAXException e) 
    {
        e.printStackTrace();

    } catch (MalformedURLException e) 
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }




}

}

4

1 回答 1

0

你的ParseData代码应该是这样的:

public class ParseData extends DefaultHandler { 

    StringBuilder elementText;
    ArrayList<String> arrtitle = new ArrayList<String>();
    ArrayList<String> arrcost = new ArrayList<String>();
    ArrayList<String> arrpubDate = new ArrayList<String>();
    ArrayList<String> arrlink = new ArrayList<String>();

    public ParseData() {
        elementText = new StringBuilder();
    }

    @Override
    public void endElement(String uri , String localName , String qName) throws SAXException {
        if ("title".equals(localName)) {
            arrtitle.add(elementText.toString().trim());
        } else if ("cost".equals(localName)) {
            arrcost.add(elementText.toString().trim());
        } else if ("pubDate".equals(localName)) {
            arrpubDate.add(elementText.toString().trim());
        } else if ("link".equals(localName)) {
            arrlink.add(elementText.toString().trim());
        }
        elementText.setLength(0);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        elementText.append(ch, start, length);
    }
}

另一方面,您应该始终在不同的线程中进行 HTTP 请求和 XML 解析(也许使用AsyncTask)。然后,一旦解析响应,更新 UI 并显示结果。你需要做很多改变才能让它运行,兄弟。

getView方法中,为什么不能将字符串设置为 TextViews)?(只是利用getItem(position)

编辑

如果您的 XML 是这样的,我将定义一个带有所有子标签的 JavaBeans 类,然后作为解析器的结果获取该类的实例列表。这是执行此操作的正确方法。

键入 JavaBeans 类以对 XML 的每个元素进行建模:

public class Type {

    int id;
    String title;
    String cost;
    String date;
    String link;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getCost() {
        return cost;
    }
    public void setCost(String cost) {
        this.cost = cost;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
}

解析器现在将构建该类的列表:

public class ParseData extends DefaultHandler { 

    StringBuilder elementText;
    List<Type> types;
    Type type;

    public ParseData() {
        types = new ArrayList<Type>();
        elementText = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("type".equals(localName)) {
            type = new Type();
        }
    }

    @Override
    public void endElement(String uri , String localName , String qName) throws SAXException {
        if ("title".equals(localName)) {
            type.setTitle(elementText.toString().trim());
        } else if ("cost".equals(localName)) {
            type.setCost(elementText.toString().trim());
        } else if ("pubDate".equals(localName)) {
            type.setDate(elementText.toString().trim());
        } else if ("link".equals(localName)) {
            type.setLink(elementText.toString().trim());
        } else if ("type".equals(localName)) {
            types.add(type);
        }
        elementText.setLength(0);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        elementText.append(ch, start, length);
    }
}

编辑 2

在您的getView()方法中,您必须启动一个从给定的(从 XML 解析的)AsycTask检索 a的方法。在方法内部,您可以使用实用方法来检索位图,在方法内部,您可以使用将检索到的位图设置为ImageView。BitmapURLdoInBackgroundBitmapFactoryonPostExecutesetImageBitmapappropiate

WeakReference注意:始终通过使用UI 小部件或在适当时取消AsyncTask实例来防止配置更改问题、滚动列表过快或类似的事情。

于 2013-02-04T13:34:18.963 回答