この1日、私はこの問題を克服しようとしてきましたが、成功しませんでした。何が起こるか-私はJsoup(getElementsByClass
)を使用して特定のクラスの下ですべての値を取得し、次にforeachループを実行し、最後にrunOnUiThread
UIのビューを更新するために使用します。
長い話、短い-何らかの理由で最初の要素は問題ありませんが、2番目の要素から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();
}