Tuesday, April 24, 2012

Populate a ListView with an AsyncTask failing to populate

Trying to get this working... it loads up fine, even tells the application that it completed getting all the data. It does not populate the listview though.



The data response inside show_list: [A, B, C, D]



public class MainActivity extends ActionBarActivity {
private static final String DEBUG_TAG = "MainActivity";
private boolean mAlternateTitle = false;
List<Show> mShowList;
AlertDialog mAlertDialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadTitles();
}

private void loadTitles() {
ShowsList show_list = new ShowsList();
show_list.execute();
}

private class ShowsList extends AsyncTask<Void, Void, List<Show>> {
@Override
protected void onPreExecute() {
mAlertDialog = new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_action_refresh).setTitle(R.string.fetching_new).show();
}

@Override
protected List<Show> doInBackground(Void... voids) {
try {
mShowList = Show.getShows();
return mShowList;
} catch (Exception e) {
new AlertDialog.Builder(MainActivity.this.getApplicationContext()).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.server_down_title).setMessage(R.string.server_down_message).setPositiveButton(R.string.app_quit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.this.finish();
}
}).show();
return null;
}
}

@Override
protected void onPostExecute(final List<Show> show_list) {
final String DEBUG_TAG = "MainActivity$ShowList$onPostExecute";
/*ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<Show> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, async_show_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new ListClickListener());
*/
if (mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}

try {
ListView lv = (ListView) findViewById(R.id.list);
ShowsAdapter adapter = new ShowsAdapter(MainActivity.this, android.R.layout.simple_list_item_1, show_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new ListClickListener());
Log.d(DEBUG_TAG, adapter.toString());
adapter.notifyDataSetChanged();
//adapter.add(show_list[0]);
} catch (Exception e) {
new AlertDialog.Builder(MainActivity.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.server_down_title).setMessage(R.string.server_down_message).setPositiveButton(R.string.app_quit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.this.finish();
}
}).show();
}
}
}

private class ListClickListener implements AdapterView.OnItemClickListener {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Show show = mShowList.get(i);
Toast.makeText(MainActivity.this, "Clicked on a list item: " + show.title, Toast.LENGTH_LONG).show();
}
}

private class ShowsAdapter extends ArrayAdapter<Show> {
public ShowsAdapter(Context context, int textViewResourceId, List<Show> shows) {
super(context, textViewResourceId, shows);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Show show = this.getItem(position);

if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_row_show, parent, false);
}

((TextView) convertView.findViewById(R.id.show_title)).setText(show.title);
//((TextView) convertView.findViewById(R.id.episode_number)).setText(episode.getGrayLine());

return convertView;
}
}




No comments:

Post a Comment