我觉得我非常接近这个工作,但到目前为止还没有雪茄。你们其中一位优秀的开发者能给我一些建议吗:P
我有一个主要活动,它为RAW文件夹中的文章提供了一个列表视图,我有一个JSONparser类来完成所有这些工作。
如下所示,我的主要活动显示了try语句List<News> news = businessParser.parseXML(this);
的这一部分中的错误,尤其是parseXML
,其中指出了更改方法以解析输入流
public class Articles_Business extends FragmentActivity {
ListView businesslv;
//Is first called when the Activity is opened
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artbusiness);
//Implementing the listeners for Application Navigation
Button btnbck=(Button)findViewById(R.id.btnbck);
btnbck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Articles_Business.this, ArticlesActivity.class);
startActivity(i);
}
});
//Implementing the functionality for the XMLParsing of data to be used
// for users to view intended articles
businesslv = (ListView) findViewById(R.id.businesslst);
try{
BusinessJSONParser businessParser = new BusinessJSONParser();
List<News> news = businessParser.parseXML(this);
news = businessParser.parseXML(getAssets().open("businessnews"));
ArrayAdapter<News> arrAdadter = new ArrayAdapter<News>(this,
R.layout.activity_artbusiness, news);
businesslv.setAdapter(arrAdadter);
} catch (IOException e){
e.printStackTrace();
}
}
但是我已经在JSONparser中声明了一个输入流 公共类BusinessJSONParser {
private static final String NEWS_ID = "newsId";
private static final String NEWS_TITLE = "newsTitle";
private static final String NEWS_CONTENT = "newsContent";
private News currNews = null;
private String currTag = null;
List<News> news = new ArrayList<News>();
public List<News> parseXML (InputStream inputStream) throws XmlPullParserException{
try{
XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
xppf.setNamespaceAware(true);
XmlPullParser xpp = xppf.newPullParser();
//opening the correct xml file for the Business Class in a
//Steaming method
InputStream inputstream = inputStream.getResources().openRawResource(R.raw.businessnews);
xpp.setInput(inputstream, null);
//parser sending events and the trigger response to open
int i = xpp.getEventType();
while (i != XmlPullParser.END_DOCUMENT) {
if (i == XmlPullParser.START_TAG) {
//System.out.println(xpp.getName());
//Returning the text content in this event via the method
//handleStartEvent()
handleStartEvent (xpp.getName());
} else if (i == XmlPullParser.END_TAG) {
currTag = null;
} else if (i == XmlPullParser.TEXT) {
//System.out.println(xpp.getName());
//Returning the text content in this event via the method
//parseContentEvent()
parseContentEvent (xpp.getText());
}
i = xpp.next();
}
} catch (IOException e) {
Log.d("Error" + e.getMessage(), currTag);
}
return news;
}
private void handleStartEvent(String name) {
if (name.equals("news")) {
currNews = new News();
news.add(currNews);
}else{
currTag = name;
}
}
private void parseContentEvent(String text) {
String sText = text;
if (currNews != null && currNews != null) {
if (currNews.equals(NEWS_ID)) {
Integer primary = Integer.parseInt(sText);
currNews.setId(primary);
}else if (currNews.equals(NEWS_TITLE)) {
currNews.setTitle(sText);
}else if (currNews.equals(NEWS_CONTENT)) {
currNews.setContent(sText);
}
}
}
}
我不认为这很重要,但该活动的.xml视图只显示了一个&#39; back&#39;按钮和列表视图名称&#39; businesslst&#39;
我已经坚持了一段时间,并将非常感谢您的意见。 三江源