嗨,我对 android 平台非常陌生 ..off 晚了,我尝试使用 xml pull 解析器解析 rss 提要。当我使用 android 的标准 listview 布局时,我的代码运行良好。但是当我尝试实现时使用自定义列表视图和适配器的相同代码..没有显示数据..我用原始数据(不是从提要中获取的数据)测试了我的自定义适配器,它工作得很好..但是当我尝试使用这些信息时从提要中获取的列表视图没有显示任何数据..已经尝试了很长一段时间但无法完成..任何人都可以帮我确定我所犯的错误...请注意我的应用程序可以完美地与非互联网数据一起运行,但不能与从提要中获取的数据一起运行..我正在尝试从提要中提取标题标签..任何帮助将不胜感激...代码如下
MainActivity.java
public class MainActivity extends Activity {
ArrayList<FeedData> headlines;
List <String>title;
FeedData data = new FeedData();
List links;
String msg =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = new ArrayList<String>();
links = new ArrayList();
headlines = new ArrayList<FeedData>();
DownloadTask runner =new DownloadTask();
runner.execute();
for(int i=0;i<title.size();i++){
msg=title.get(i).toString();
data.setTitle(msg);
headlines.add(data);
}
FeedAdapter adapter= new FeedAdapter(MainActivity.this,R.layout.imagefeed,headlines);
ListView list = (ListView)findViewById(R.id.lvMyFeed);
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class DownloadTask extends AsyncTask<String,Integer,String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{ URL url = new URL("http://www.pcworld.com/index.rss ");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
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()); //extract the headline
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
}// end of DownloadTask class
}`
FeedData.java
public class FeedData {
String headlines;
public FeedData()
{
}
public FeedData(String title) {
this.headlines=title;
}
public void setTitle(String title){
this.headlines=title;
}
public String getTitle(){
return headlines;
}
}`
`FeedData.java
public class FeedData {
String headlines;
public FeedData()
{
}
public FeedData(String title) {
this.headlines=title;
}
public void setTitle(String title){
this.headlines=title;
}
public String getTitle(){
return headlines;
}
}
activity_main.xml
<ListView
android:id="@+id/lvMyFeed"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
图片提要.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ivImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="38dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tvFeed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ivImg"
android:layout_marginLeft="78dp"
android:layout_marginTop="14dp"
android:layout_toRightOf="@+id/ivImg"
android:text="Headlines" />
</RelativeLayout>