Good day. Required to parse articles and display them in a listView
. Parsing is OK, the problem starts when you need to display articles in the listView. It turns out that the adapter loads the arr
array even before Jsoup
spars, that is, the array is empty at this moment. Tried to make the assignment of the adapter lv.setAdapter(adapter);
in onPostExecute
, but there the setAdapter
"does not see" adapter
. Please tell me how to solve the problem. Thank.
import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import java.io.IOException; import java.util.ArrayList; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class MainActivity extends AppCompatActivity { TextView textView; String text; Button button; Parser par; ListView lv; ArrayList<String> arr = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); lv = (ListView)findViewById(R.id.listView); par = new Parser(); par.execute(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr); lv.setAdapter(adapter); } class Parser extends AsyncTask<Void, Void, Void> { protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void ... params) { Document doc = null; try { doc = Jsoup.connect("http://site.ru").get(); } catch (IOException e) { e.printStackTrace(); } if (doc != null) { Elements text2 = doc.select("div.entry-content"); for (Element temp : text2) { arr.add(temp.text()); } } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } } }