gpt4 book ai didi

java - ArrayList 不显示数据存储发生的方法之外的值

转载 作者:行者123 更新时间:2023-12-01 18:03:43 25 4
gpt4 key购买 nike

我有这个类,它处理 XML 并将其存储在 ArrayList<FeedItem> 中。我可以在存储数据的方法中显示数组内容,但是当我尝试在另一种方法中显示数组时,它没有通过 if检查表明 ArrayList 为空。因此,我无法创建 ListView,因为它会返回相同的错误。我希望有人能简单地向我解释一下哪里出了问题。

读取RSS.java

public class ReadRSS extends AsyncTask<Void, Void, Void> {

//Initialize progress dialog
Context context;
String address;
ProgressDialog progressDialog;
XmlPullParserFactory xmlPullParserFactory;
volatile boolean parsingComplete = true;
ArrayList<FeedItem> feedItems;
ListView listView;

public ReadRSS(Context context, ListView listView, String retrieveAddress) {
//Create a new progress dialog
this.listView = listView;
this.address = retrieveAddress;
this.context = context;
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading....");

}

// Runs in UI before background thread is called
@Override
protected void onPreExecute() {
//Display progress dialog
progressDialog.show();
super.onPreExecute();
}

// This is run in a background thread
@Override
protected Void doInBackground(Void... voids) {
fetchXML();
return null;
}

// This is called from background thread but runs in UI
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

// This runs in UI when background thread finishes
@Override
protected void onPostExecute(Void aVoid) {
//Dismiss progress dialog
super.onPostExecute(aVoid);
progressDialog.dismiss();


/*if(listView != null) {
CustomAdapter customAdapter = new CustomAdapter(context, R.layout.activity_listview, feedItems);
listView.setAdapter(customAdapter);

}*/

if(feedItems != null){
//Gives error
for(int i = 0; i < feedItems.size(); i++) {
Log.d("Title", feedItems.get(i).getTitle());
Log.d("Date", feedItems.get(i).getPubDate());
}

}

}

//New Build

public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text;
String title = null;
String date = null;

feedItems = new ArrayList<FeedItem>();

try {
event = myParser.getEventType();

while (event != XmlPullParser.END_DOCUMENT) {
String tagName = myParser.getName();

switch (event){

case XmlPullParser.START_TAG:
if(tagName.equalsIgnoreCase("item")){
int eventChild = myParser.next();
//int innerLoop = 1;

String tagNameChild = "";
while(eventChild != XmlPullParser.END_DOCUMENT){

if(eventChild == XmlPullParser.START_TAG){
tagNameChild = myParser.getName();
// Output Test
//Log.d("Tag ", tagNameChild);
}

else if (eventChild == XmlPullParser.TEXT){

text = myParser.getText();
// Output Test
//Log.d("Test ", text);


if(tagNameChild.equalsIgnoreCase("title")){

title = text;
// Output Test
//Log.d("Title ", myParser.getText());
}
else if(tagNameChild.equalsIgnoreCase("pubDate")){

date = text;
// Output Test
//Log.d("PubDate ", myParser.getText());

}
}

else if (eventChild == XmlPullParser.END_TAG){


if(myParser.getName().equalsIgnoreCase("item")){

feedItems.add(new FeedItem(title,date));
// Output Test
//Log.d("Test ", title);
}

tagNameChild = "";

}

eventChild = myParser.next();

//innerLoop++;
}

//Output Test
/*for(int i = 0; i < feedItems.size(); i++) {
Log.d("Title", feedItems.get(i).getTitle());
Log.d("Date", feedItems.get(i).getPubDate());
}*/
}

break;

case XmlPullParser.TEXT:
break;

case XmlPullParser.END_TAG:
break;

}

event = myParser.next();

}

parsingComplete = false;
}

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

public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {

try {
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(10000 );
conn.setConnectTimeout(15000 );
conn.setRequestMethod("GET");
conn.setDoInput(true);

// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();

xmlPullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlPullParserFactory.newPullParser();

myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);

parseXMLAndStoreIt(myparser);
stream.close();
}

catch (Exception e) {
}
}
});
thread.start();
}
}

最佳答案

您正在从 doInBackground 调用 fetchXML(),但 fetchXML() 启动一个新线程,然后立即返回。然后doInBackground()立即返回并调用onPostExecute()。但是,此时由 fetchXML() 启动的线程还没有时间完成,因此 feedItems 尚未正确设置。

这是使用 AsyncTask 的错误方法。相反,您应该直接在 doInBackground() 线程中进行提取。只需重写 fetchXML() 即可自行获取数据,而不是启动单独的线程来进行获取。

关于java - ArrayList 不显示数据存储发生的方法之外的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38580470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com