在过去的一天里,我一直在努力克服这个问题,但我没有成功。发生的事情是 - 我正在使用 Jsoup ( getElementsByClass
) 获取某个类下的所有值,然后我正在运行一个 foreach 循环,最后,我使用runOnUiThread
它来更新 UI 上的视图。
长话短说 - 出于某种原因,第一个元素没问题,但从第二个元素到第 10 个元素(有 10 个),它只使用第 10 个元素的值。当我取下 runOnUiThread 并将其用作常规函数(而不是在我需要的线程内)时,它可以完美运行并且不会跳过/复制任何元素。
所以,在这个很长的故事之后(对不起,我没有把它说短),这是我的代码:
counter = 1;
AssetManager assets = getAssets();
font = Typeface.createFromAsset(assets, "arial.ttf");
//Loading 10 gags
try {
Document doc = Jsoup.connect(url).get();
Elements content = doc.getElementsByClass("Gag-Content");
for(Element element : content) {
data = element.outerHtml();
if(data.indexOf("<iframe") == -1){
//Long ass random code here, in the end there are 2 variables: imageUrl & imageText
switch(counter){
case 1:
runOnUiThread(new Runnable() {
public void run() {
final TextView text1;
final ImageView image1;
text1 = (TextView)((Activity) context).findViewById(R.id.textView1);
text1.setText(imageText);
text1.setTypeface(font);
image1 = (ImageView)((Activity) context).findViewById(R.id.imageView1);
image1.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text1;
final ImageView image1;
text1 = (TextView)((Activity) context).findViewById(R.id.textView1);
text1.setText(imageText);
text1.setTypeface(font);
image1 = (ImageView)((Activity) context).findViewById(R.id.imageView1);
image1.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 2:
runOnUiThread(new Runnable() {
public void run() {
final TextView text2;
final ImageView image2;
text2 = (TextView)((Activity) context).findViewById(R.id.textView2);
text2.setText(imageText);
text2.setTypeface(font);
image2 = (ImageView)((Activity) context).findViewById(R.id.imageView2);
image2.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text2;
final ImageView image2;
text2 = (TextView)((Activity) context).findViewById(R.id.textView2);
text2.setText(imageText);
text2.setTypeface(font);
image2 = (ImageView)((Activity) context).findViewById(R.id.imageView2);
image2.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 3:
runOnUiThread(new Runnable() {
public void run() {
final TextView text3;
final ImageView image3;
text3 = (TextView)((Activity) context).findViewById(R.id.textView3);
text3.setText(imageText);
text3.setTypeface(font);
image3 = (ImageView)((Activity) context).findViewById(R.id.imageView3);
image3.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text3;
final ImageView image3;
text3 = (TextView)((Activity) context).findViewById(R.id.textView3);
text3.setText(imageText);
text3.setTypeface(font);
image3 = (ImageView)((Activity) context).findViewById(R.id.imageView3);
image3.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 4:
runOnUiThread(new Runnable() {
public void run() {
final TextView text4;
final ImageView image4;
text4 = (TextView)((Activity) context).findViewById(R.id.textView4);
text4.setText(imageText);
text4.setTypeface(font);
image4 = (ImageView)((Activity) context).findViewById(R.id.imageView4);
image4.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text4;
final ImageView image4;
text4 = (TextView)((Activity) context).findViewById(R.id.textView4);
text4.setText(imageText);
text4.setTypeface(font);
image4 = (ImageView)((Activity) context).findViewById(R.id.imageView4);
image4.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 5:
runOnUiThread(new Runnable() {
public void run() {
final TextView text5;
final ImageView image5;
text5 = (TextView)((Activity) context).findViewById(R.id.textView5);
text5.setText(imageText);
text5.setTypeface(font);
image5 = (ImageView)((Activity) context).findViewById(R.id.imageView5);
image5.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text5;
final ImageView image5;
text5 = (TextView)((Activity) context).findViewById(R.id.textView5);
text5.setText(imageText);
text5.setTypeface(font);
image5 = (ImageView)((Activity) context).findViewById(R.id.imageView5);
image5.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 6:
runOnUiThread(new Runnable() {
public void run() {
final TextView text6;
final ImageView image6;
text6 = (TextView)((Activity) context).findViewById(R.id.textView6);
text6.setText(imageText);
text6.setTypeface(font);
image6 = (ImageView)((Activity) context).findViewById(R.id.imageView6);
image6.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text6;
final ImageView image6;
text6 = (TextView)((Activity) context).findViewById(R.id.textView6);
text6.setText(imageText);
text6.setTypeface(font);
image6 = (ImageView)((Activity) context).findViewById(R.id.imageView6);
image6.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 7:
runOnUiThread(new Runnable() {
public void run() {
final TextView text7;
final ImageView image7;
text7 = (TextView)((Activity) context).findViewById(R.id.textView7);
text7.setText(imageText);
text7.setTypeface(font);
image7 = (ImageView)((Activity) context).findViewById(R.id.imageView7);
image7.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text7;
final ImageView image7;
text7 = (TextView)((Activity) context).findViewById(R.id.textView7);
text7.setText(imageText);
text7.setTypeface(font);
image7 = (ImageView)((Activity) context).findViewById(R.id.imageView7);
image7.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 8:
runOnUiThread(new Runnable() {
public void run() {
final TextView text8;
final ImageView image8;
text8 = (TextView)((Activity) context).findViewById(R.id.textView8);
text8.setText(imageText);
text8.setTypeface(font);
image8 = (ImageView)((Activity) context).findViewById(R.id.imageView8);
image8.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text8;
final ImageView image8;
text8 = (TextView)((Activity) context).findViewById(R.id.textView8);
text8.setText(imageText);
text8.setTypeface(font);
image8 = (ImageView)((Activity) context).findViewById(R.id.imageView8);
image8.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 9:
runOnUiThread(new Runnable() {
public void run() {
final TextView text9;
final ImageView image9;
text9 = (TextView)((Activity) context).findViewById(R.id.textView9);
text9.setText(imageText);
text9.setTypeface(font);
image9 = (ImageView)((Activity) context).findViewById(R.id.imageView9);
image9.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text9;
final ImageView image9;
text9 = (TextView)((Activity) context).findViewById(R.id.textView9);
text9.setText(imageText);
text9.setTypeface(font);
image9 = (ImageView)((Activity) context).findViewById(R.id.imageView9);
image9.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
case 10:
runOnUiThread(new Runnable() {
public void run() {
final TextView text10;
final ImageView image10;
text10 = (TextView)((Activity) context).findViewById(R.id.textView10);
text10.setText(imageText);
text10.setTypeface(font);
image10 = (ImageView)((Activity) context).findViewById(R.id.imageView10);
image10.setImageBitmap(getBitmapFromURL(imageUrl));
}
});
/* final TextView text10;
final ImageView image10;
text10 = (TextView)((Activity) context).findViewById(R.id.textView10);
text10.setText(imageText);
text10.setTypeface(font);
image10 = (ImageView)((Activity) context).findViewById(R.id.imageView10);
image10.setImageBitmap(getBitmapFromURL(imageUrl)); */
break;
}
counter++;
if(counter >= 11){
pd.dismiss();
break;
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}