Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 显示异步任务中卡的详细信息_Xml_Asynchronous_Google Glass_Google Gdk - Fatal编程技术网

Xml 显示异步任务中卡的详细信息

Xml 显示异步任务中卡的详细信息,xml,asynchronous,google-glass,google-gdk,Xml,Asynchronous,Google Glass,Google Gdk,我试图使用SAX解析器从XML中提取数据。我已经在一个异步任务中实现了SAX解析器。正如我为谷歌眼镜开发的一样,与卡片相比,列表视图中的android代码对用户并不友好 我如何获得要在卡片上显示的解析信息,以及卡片的创建应该在哪里?我尝试将它们包括在异步任务中,但失败了 活动类 public class Dpublic class DpadInputActivity extends Activity { ListView lvPcsPost; private List<Po

我试图使用SAX解析器从XML中提取数据。我已经在一个异步任务中实现了SAX解析器。正如我为谷歌眼镜开发的一样,与卡片相比,列表视图中的android代码对用户并不友好

我如何获得要在卡片上显示的解析信息,以及卡片的创建应该在哪里?我尝试将它们包括在异步任务中,但失败了

活动类

public class Dpublic class DpadInputActivity extends Activity {
    ListView lvPcsPost;
    private List<PostValue> mCards;
    private CardScrollView mCardScrollView;
    private Context context;
    static ArrayList<PostValue> postValueArrayList;

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

    createCards();

    mCardScrollView = new CardScrollView(this);
    PostBaseAdapter adapter = new PostBaseAdapter(context, mCards);
    mCardScrollView.setAdapter(adapter);
    mCardScrollView.activate();
    setContentView(mCardScrollView);
    setContentView(R.layout.activity_main1);

    new PostAsync().execute();
    }

    class PostAsync extends AsyncTask<Void, Void, Void> {
        ProgressDialog pd;
        XMLHelper helper;

        @Override
        protected void onPreExecute() {
            pd = ProgressDialog.show(DpadInputActivity.this, "xx", "Loading
             posts ...", true, false);
        }

        //reading and parsing
        @Override
        protected Void doInBackground(Void... arg0) {
            helper = new XMLHelper();
            helper.get();
            postValueArrayList = helper.getPostsList();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            PostBaseAdapter postBaseAdapter = new
               PostBaseAdapter(DpadInputActivity.this, postValueArrayList);
            lvPcsPost.setAdapter(postBaseAdapter);
            pd.dismiss();
        }
     }

    private void createCards() {
        mCards = new ArrayList<PostValue>();
        for (int i = 0; i < postValueArrayList.size(); i++) {
            mCards.add(postValueArrayList.get(i));
         }
    }
}
公共类Dpublic类DpadInputActivity扩展活动{
ListView lvPcsPost;
私有列表mCards;
私有卡滚动视图mCardScrollView;
私人语境;
静态ArrayList postValueArrayList;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
上下文=这个;
createCards();
mCardScrollView=新的CardScrollView(此);
PostBaseAdapter=新的PostBaseAdapter(上下文,mCards);
mCardScrollView.setAdapter(适配器);
mCardScrollView.activate();
setContentView(mCardScrollView);
setContentView(R.layout.activity_main1);
新建PostAsync().execute();
}
类PostAsync扩展异步任务{
进展性帕金森病;
XMLHelper助手;
@凌驾
受保护的void onPreExecute(){
pd=ProgressDialog.show(DpadInputActivity.this,“xx”,“加载
帖子…,对,错);
}
//阅读和分析
@凌驾
受保护的Void doInBackground(Void…arg0){
helper=新的XMLHelper();
helper.get();
postValueArrayList=helper.getPostsList();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
PostBaseAdapter PostBaseAdapter=新建
PostBaseAdapter(DpadInputActivity.this,postValueArrayList);
lvPcsPost.setAdapter(postBaseAdapter);
pd.解散();
}
}
私人信用卡(){
mCards=newarraylist();
对于(int i=0;i
XML助手类

public class XMLHelper extends DefaultHandler{
    private String URL_MAIN = "blabla.xml";
    String TAG = "XMLHelper";
    Boolean currTag = false;
    String currTagVal = "";

    private PostValue post = null;
    private ArrayList<PostValue> posts = new ArrayList<PostValue>();

    public ArrayList<PostValue> getPostsList() {
        return this.posts;
    }

    public void get() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser mSaxParser = factory.newSAXParser();
            XMLReader mXmlReader = mSaxParser.getXMLReader();
            mXmlReader.setContentHandler(this);

            InputStream mInputStream = new URL(URL_MAIN).openStream();
            mXmlReader.parse(new InputSource(mInputStream));
        } catch (Exception e) {
            // Exceptions can be handled for different types
            // But, this is about XML Parsing not about Exception Handling
           Log.e(TAG, "Exception: " + e.getMessage());
        }
     }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (currTag) {
            currTagVal = currTagVal + new String(ch, start, length);
            currTag = false;
        }
    }
    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        Log.i(TAG, "TAG: " + localName);
        currTag = true;
        currTagVal = "";
        if (localName.equals("marker"))
            post = new PostValue();
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
         currTag = false;

        if (localName.equalsIgnoreCase("location")) //post_title
             post.setLocation(currTagVal);

        else if (localName.equalsIgnoreCase("devt_type")) //guild
            post.setType(currTagVal);

        else if (localName.equalsIgnoreCase("gpr")) //post_date
            post.setGpr(currTagVal);

        else if (localName.equalsIgnoreCase("marker")) //end of tag; adding    object to list
        posts.add(post);
    }
}
公共类XMLHelper扩展了DefaultHandler{
私有字符串URL_MAIN=“blabla.xml”;
String TAG=“XMLHelper”;
布尔currTag=false;
字符串currTagVal=“”;
私有PostValue post=null;
private ArrayList posts=new ArrayList();
公共阵列列表getPostsList(){
把这封信寄回去;
}
公开作废获取(){
试一试{
SAXParserFactory=SAXParserFactory.newInstance();
SAXParser mSaxParser=factory.newSAXParser();
XMLReader mXmlReader=mSaxParser.getXMLReader();
mXmlReader.setContentHandler(这个);
InputStream mInputStream=新URL(URL_MAIN).openStream();
parse(新输入源(mInputStream));
}捕获(例外e){
//可以针对不同类型处理异常
//但是,这是关于XML解析的,而不是关于异常处理的
Log.e(标记,“异常:+e.getMessage());
}
}
@凌驾
公共无效字符(字符[]ch,整数开始,整数长度)
抛出SAX异常{
if(currTag){
currTagVal=currTagVal+新字符串(ch,start,length);
currTag=false;
}
}
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、,
属性)引发SAX异常{
Log.i(标签,“标签:”+localName);
currTag=true;
currTagVal=“”;
if(localName.equals(“标记”))
post=新的PostValue();
}
@凌驾
公共void endElement(字符串uri、字符串localName、字符串qName)
抛出SAX异常{
currTag=false;
if(localName.equalsIgnoreCase(“location”)//post\u title
邮政定位(currTagVal);
else if(localName.equalsIgnoreCase(“devt_type”)//guild
post.setType(currTagVal);
else if(localName.equalsIgnoreCase(“gpr”)//发布日期
邮政总局(currTagVal);
else if(localName.equalsIgnoreCase(“marker”)//标记结束;将对象添加到列表
增加(员额);
}
}

出于某些原因,我对XML的URL保密

如果您正在异步任务中提取数据并希望在UI上显示数据,则需要返回信息。您没有从AsyncTask内部访问UI线程的权限


至于卡的实际构建,您需要创建一个实现CardScrollAdapter的类。您需要重写getView方法,然后用传递到适配器的数据填充视图。请确保也查看视图持有者。

您能提供一些代码片段以便我更好地理解吗?我想知道这是最有效的方法吗,因为XML文件很大。