Я пытаюсь понять это часами.У меня есть ArrayLIst с именем imgURLS, в который я хочу добавить URL-адреса, которые я проанализировал со своего веб-сайта с помощью jsoup.Но imgURLS всегда пуст.Я попробовал анализ jsoup на затмении, и это сработало.Массив работает, когда я добавляю строку в метод create, но не в URLcollector (который является реализацией Async).
Что мой код должен сделать, так это сначала получить URL-адреса изображений с моего веб-сайта.Затем поместите их в arrayLIst.затем используйте элементы (ссылки) arrayList, которые будут отображаться в imageView.
Теперь все остальное работает, кроме получения ссылок на анализируемое изображение для сохранения в arrayList.Мне нужна помощь, чтобы сделать это.Я открыт для использования другого контейнера, например, arrayList, если это облегчает задачу.Спасибо
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
ImageView imageView;
int count = 0;
String websteURL = "https://wubewallpapers.wordpress.com/";
//String url1 = "https://wubewallpapers.files.wordpress.com/2018/03/landscape.jpg?w=134&h=201&zoom=2";
ArrayList<String> imgURLS = new ArrayList<>();
//ArrayList<String> imgURLS2 = new ArrayList<>();
//ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout. ,imgURLS );
Button setBKG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
setBKG = findViewById(R.id.setBKG);
new URLcollector().execute(websteURL);
loadImageFromUrl(imgURLS.get(count));
imageView.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
public void onSwipeRight() {
Toast.makeText(MainActivity.this, "right : " + imgURLS.size(), Toast.LENGTH_SHORT).show();
if (count == imgURLS.size() - 1) {
count = 0;
} else {
count++;
}
loadImageFromUrl(imgURLS.get(count));
}
public void onSwipeLeft() {
// Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
if (count == 0) {
count = imgURLS.size() - 1;
} else {
count -= 1;
}
loadImageFromUrl(imgURLS.get(count));
}
});
}
public void loadImageFromUrl(String url) {
Picasso.with(this).load(url).placeholder(R.mipmap.ic_launcher) // optional
.error(R.mipmap.ic_launcher) //if error
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
// if user clicked on set wallpaper
// then set wallpaper
setBKG.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(bitmap);
Toast.makeText(MainActivity.this, "wallpaper changed", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(MainActivity.this, "There was an error", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public void onError() {
}
});
}
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context ctx) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
private class URLcollector extends AsyncTask<String, Void, ArrayList<String>> {
private static final String TAG = "MainActivity";
ArrayList<String> imgURLS2 = new ArrayList<>();
// these Strings / or String are / is the parameters of the task, that can be handed over via the excecute(params) method of AsyncTask
protected ArrayList<String> doInBackground(String... params) {
String param1 = params[0];
try {
Document document = Jsoup
.connect("https://wubewallpapers.wordpress.com/").get();
Elements spanElements = document.select("img");
for (Element imgElement : spanElements) {
imgURLS2.add(imgElement.attr("src"));
}
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
// the Integer variable is used for progress
publishProgress();
// once the data is downloaded (for example JSON data)
// parse the data and return it to the onPostExecute() method
// in this example the return data is simply a long value
// this could also be a list of your custom-objects, ...
return imgURLS;
}
// the onPostexecute method receives the return type of doInBackGround()
@Override
protected void onPostExecute(ArrayList<String> result) {
// do something with the result, for example display the received Data in a ListView
// in this case, "result" would contain the "someLong" variable returned by doInBackground();
imgURLS = imgURLS2;
}
}
}
ошибка
------- beginning of crash
05-28 20:52:04.514 19691-19691/com.wube.wubealpha E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wube.wubealpha, PID: 19691
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wube.wubealpha/com.wube.wubealpha.MainActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:411)
at com.wube.wubealpha.MainActivity.onCreate(MainActivity.java:64)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
05-28 20: 52