Android 在线程中从xml获取数据?
我正在尝试从xml中获取图像和文本。。。但我希望这个过程发生在后台,所以我尝试使用一个线程,我可以将数据放入一些数组中,然后我可以用这个数组设置imageView和textView。当我运行时,我看到程序在线程中运行良好,获取数据并放入数组。。。但是在setText和setImageBitmap上,我看到那些数组是空的?!怎么会这样 我基本上遵循本教程从Xml获取数据: 这是我的密码:Android 在线程中从xml获取数据?,android,multithreading,runnable,Android,Multithreading,Runnable,我正在尝试从xml中获取图像和文本。。。但我希望这个过程发生在后台,所以我尝试使用一个线程,我可以将数据放入一些数组中,然后我可以用这个数组设置imageView和textView。当我运行时,我看到程序在线程中运行良好,获取数据并放入数组。。。但是在setText和setImageBitmap上,我看到那些数组是空的?!怎么会这样 我基本上遵循本教程从Xml获取数据: 这是我的密码: public class mainPage { HashMap<Integer, String>
public class mainPage {
HashMap<Integer, String> desc;
HashMap<Integer, Bitmap> bitMap;
ScrollView scroll;
View app, temp;
static int j = 0;
static View first;
static NodeList nodes;
static TextView textView, textView1, textView2;
static ImageView img, image1, image2;
LinearLayout parent;
HashMap<Integer, View> others;
Runnable get;
public mainPage() {
this.app = MainActivity.app;
this.scroll = (ScrollView) app.findViewById(R.id.mainPage_scroll);
this.parent = new LinearLayout(scroll.getContext());
parent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
parent.setOrientation(LinearLayout.VERTICAL);
scroll.addView(parent);
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
others = new HashMap<Integer, View>();
// add first item to main page
first = inflater.inflate(R.layout.main_page_first_item, null);
parent.addView(first);
others.put(0, first);
for (int i = 2; i < 10; i++) {
temp = inflater.inflate(R.layout.main_page_item, null);
others.put(i, temp);
parent.addView(temp);
}
}
public void setPage() {
// take datas from XML
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
// which datas need to be taken, by tag
nodes = doc.getElementsByTagName("image");
desc = new HashMap<Integer, String>();
bitMap = new HashMap<Integer, Bitmap>();
new Thread(new Runnable() {
public void run() {
for (j = 0; j < 20; j++) {
if (j != 1) {
Element e = (Element) nodes.item(j);
desc.put(j, XMLfunctions.getValue(e, "description"));
Log.d("try", desc.get(j));
Log.d("tryy",
XMLfunctions.getValue(e, "description"));
bitMap.put(j, XMLfunctions.getContactPhoto(XMLfunctions
.getValue(e, "path")));
}
}
}
}).start();
// add the first item to main page
textView = (TextView) (others.get(0)).findViewById(R.id.text);
img = (ImageView) (others.get(0)).findViewById(R.id.image);
Log.d("first", desc.get(0) + desc.get(0));
if (textView != null)
textView.setText(desc.get(0));
if (img != null)
img.setImageBitmap(bitMap.get(0));
// add the rest of items to main page
for (j = 2; j < 10; j++) {
if (j % 2 == 0) {
textView1 = (TextView) (others.get(j)).findViewById(R.id.text1);
image1 = (ImageView) (others.get(j)).findViewById(R.id.image1);
Log.d("others", j + ": " + desc.get(j) + desc.get(j));
if (textView1 != null)
textView1.setText(desc.get(j));
if (image1 != null)
image1.setImageBitmap(bitMap.get(j));
} else {
textView2 = (TextView) (others.get(j - 1))
.findViewById(R.id.text2);
image2 = (ImageView) (others.get(j - 1))
.findViewById(R.id.image2);
Log.d("others", j + ": " + desc.get(j) + desc.get(j));
if (textView2 != null)
textView2.setText(desc.get(j));
if (image2 != null)
image2.setImageBitmap(bitMap.get(j));
}
}
}
}
AsyncTask是您想要使用的请查看以下示例:
其中两种方法在线程任务之前和之后触发,因此您应该能够使用它来获取数据。
onPostExecute
在doInBackground
方法完成实际工作后触发onPostExecute
获取doInBackground
返回的任何内容…我也尝试过,但无法完成,所以我只是尝试了这种方式,但奇怪的是,它没有改变数组的值。。。转动null@yahya你能举个例子吗?后台进程可能会抛出错误吗?@yahya,为什么要写入静态变量?在“doInBackground”中,使用局部变量,返回包含这两个数组的类,然后在onPostExecute中使用该类。。。实际上,您正在跨多个线程访问一个静态实例,这可能是问题所在,但不是100%确定。返回包含这两个数组的类是什么意思?
mainPage fillMain = new mainPage();
fillMain.setPage();