Я использую Jsoup для получения названий песен в виде строк с моего сайта. Я хочу, чтобы эти песни были показаны в ArrayAdapter. Песни хранятся в типе данных ArrayList, который необходим ArrayAdapter в качестве параметра. Имена песен все правильно получены с использованием Jsoup, однако код для отображения содержимого ArrayList, в котором хранятся названия песен, не обновляет пользовательский интерфейс соответствующим образом.
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
public class AppHome extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {
TextView texx;
ArrayList<String> aList = new ArrayList<>();
private ArrayAdapter<String> arrayAdapter;
private int i;
AsyncTask<?,?,?> runningTask;
String[] attributeList = {"song1", "song2", "song3", "song4", "song5", "song6", "song7", "song8", "song9", "song10", "song11", "song12", "song13", "song14", "song15", "song16", "song17", "song18", "song19", "song20", "song21", "song22", "song23", "song24", "song25", "song26", "song27", "song28", "song29", "song30", "song31", "song32", "song33", "song34", "song35", "song36", "song37", "song38", "song39", "song40"};
String[] obtainedSongNamesFromAsyncTask = new String[40];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_home);
texx = findViewById(R.id.text1);
runningTask = new doit();
runningTask.execute();
}
private final class doit extends AsyncTask<Object, Void, String>
{
@Override
protected String doInBackground(Object... params)
{
try
{
Document doc = Jsoup.connect("https://screenscrape4top40.000webhostapp.com/").userAgent("Mozilla/(Windows NT 6.1)").get();
// Elements firstSong = doc.select("a[id=song1]");
// String firstSongString = firstSong.text();
// Log.d("jsoupTag", firstSongString);
// TODO 1. loop through every element in array
// TODO 2. Create new string by concatenating the .select() contents (pre & post)
// TODO 3. Add to wrapper class array
for (int i = 0; i != attributeList.length; i++)
{
String s = "a[id=" + attributeList[i] + "]";
Elements currentSong = doc.select(s);
String currentSongTextOnly = currentSong.text();
aList.add(currentSongTextOnly);
}
Log.d("jsoupTag", Arrays.toString(aList.toArray()));
// Outputs: [ROSES, BLINDING LIGHTS, THE BOX, LONELY, PHYSICAL, DON'T START NOW, SAY SO, RAIN, INTENTIONS, GODZILLA, BOYFRIEND, NO TIME TO DIE, ADORE YOU, DANCE MONKEY, BETTER OFF WITHOUT YOU, YOU SHOULD BE SAD, IN YOUR EYES, STUPID LOVE, DEATH BED, FALLING, SUPALONELY, BLUEBERRY FAYGO, AFTER HOURS, TO DIE FOR, BEFORE YOU GO, CITY OF ANGELS, WHAT A MAN GOTTA DO, KNOW YOUR WORTH, OWN IT, SOMEONE YOU LOVED, TEQUILA, BOSS BITCH, NO JUDGEMENT, FLOWERS, BIRTHDAY, SUNDAY BEST, SCORPION, THE TAKE, WAR, PLAY PLAY]
}
catch (Exception e)
{
e.printStackTrace();
}
return "Executed";
}
@Override
public void onPostExecute(String result)
{
arrayAdapter = new ArrayAdapter<>(AppHome.this, R.layout.item, R.id.helloText, aList);
SwipeFlingAdapterView flingContainer = findViewById(R.id.frame);
registerForContextMenu(flingContainer);
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
aList.remove(0);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
Toast.makeText(AppHome.this, "left", Toast.LENGTH_SHORT).show();
}
@Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(AppHome.this, "right", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
// Ask for more data here
aList.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
@Override
public void onScroll(float scrollProgressPercent) {
}
});
}
}
public void showPopup (View v){
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.dropdown_menu1);
popup.show();
}
@Override
public boolean onMenuItemClick (MenuItem item){
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "Item 1 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Item 2 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "Item 3 clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item4:
Toast.makeText(this, "Item 4 clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
}