Im trying to return an ArrayList from the AsyncTask class back to the MainAcitivity class and then use this arraylist to fill the gridview in MainActivity.
The parseURL takes a String paramater to parse the url. And parseURL is executed when the user clicks the button. The code i have compiles and run but the gridview is not populated after triggering the button event, and pressing the button twice crashes the app.
Here is my code for MainActivity:
private List<String> list = new ArrayList<String>();
public void setList(List<String> list) { this.list = list; }
public void onButtonClick(View v) {
EditText textInput = (EditText)findViewById(R.id.editText1);
String code = textInput.getText().toString();
new parseURL(this).execute(code);
//use the list from parseURL to fill grid view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
ParseURL class
public class parseURL extends AsyncTask<String, Void, List<String>> {
List<String> str;
private MainActivity act;
public parseURL(MainActivity act) {
this.act = act;
}
protected List<String> doInBackground(String... params) {
try {
Document doc = Jsoup.connect("http://ift.tt/1wlNI8G" + params).get();
Elements row1 = doc.select("table");
Elements row2 = doc.select("td");
Elements row3 = doc.select("td");
for (int i = 0; i < row1.size(); i++) {
str.add(row1.get(i).text() + "," + row2.get(i).text() + "," + row2.get(i).text());
}
return str;
}
catch() {
}
protected void onPostExecute(List<String> str) {
act.setList(str)
}
}
Aucun commentaire:
Enregistrer un commentaire