英文:
Array not being shown in ArrayAdapter when using ASyncTask
问题
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
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();
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()));
} 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);
flingContainer.setAdapter(arrayAdapter);
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
Log.d("LIST", "removed object!");
aList.remove(0);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object 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) {
aList.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;
}
@Override
public void onScroll(float scrollProgressPercent) {
}
});
}
}
// ... (remaining code omitted)
}
英文:
I'm using Jsoup to get song names as strings from my website. I want these songs to be shown in an ArrayAdapter. The songs are stored in an ArrayList data type which is what the ArrayAdapter needs as a parameter. The song names are all properly obtained using Jsoup, however the code to display the contents of an ArrayList where the song names are stored does not update the UI accordingly.
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;
}
}
}
答案1
得分: 0
请在runOnUiThread内设置Adapter
runOnUiThread(new Runnable() {
@Override
public void run() {
flingContainer.setAdapter(arrayAdapter);
}
});
并检查数组中是否包含值。希望这可以解决你的问题。
英文:
Please set Adapter inside of runOnUiThread
runOnUiThread(new Runnable() {
@Override
public void run() {
flingContainer.setAdapter(arrayAdapter);
}
});
and check if the array contain values in it. Hopes this may solve your problem
专注分享java语言的经验与见解,让所有开发者获益!
评论