试图让这个工作......它加载正常,甚至告诉应用程序它已完成获取所有数据。但它不会填充列表视图。
mArrayList.toString() 内部的数据响应:[A, B, C, D]
public class MainActivity extends ActionBarActivity {
private static final String DEBUG_TAG = "MainActivity";
private boolean mAlternateTitle = false;
ListView lv;
private ArrayList<Show> mArrayList;
ShowsAdapter adapter;
AlertDialog mAlertDialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mArrayList = new ArrayList<Show>();
lv = (ListView) findViewById(R.id.list);
adapter = new ShowsAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mArrayList);
ShowsList show_list = new ShowsList();
show_list.execute();
lv.setAdapter(adapter);
lv.setOnItemClickListener(new ListClickListener());
}
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) {
final String DEBUG_TAG = "MainActivity$ShowList$doInBackground";
try {
for (Show show : Show.getShows()) {
Log.d(DEBUG_TAG, show.toString());
mArrayList.add(show);
};
return mArrayList;
} 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) {
if (mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}
adapter.notifyDataSetChanged();
}
}
private class ListClickListener implements AdapterView.OnItemClickListener {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Show show = mArrayList.get(i);
Toast.makeText(MainActivity.this, "Clicked on a list item: " + show.title, Toast.LENGTH_LONG).show();
}
}
private class ShowsAdapter extends ArrayAdapter<Show> {
final String DEBUG_TAG = "MainActivity$ShowsAdapter";
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);
//Log.d(DEBUG_TAG, (String)((TextView) convertView.findViewById(R.id.show_title)).getText());
//((TextView) convertView.findViewById(R.id.episode_number)).setText(episode.getGrayLine());
return convertView;
}
}
以防万一布局[main.xml]有问题:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</FrameLayout>
list_show_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:textSize="17.0dip"
android:textStyle="bold"
android:textColor="#ff000000"
android:gravity="center_vertical"
android:id="@+id/show_title"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:text="Show Title"
android:layout_weight="1.0"
/>
<TextView
android:textStyle="italic" android:textColor="#ff666666"
android:id="@+id/episode_number"
android:layout_width="fill_parent" android:layout_height="0.0dip" android:text="Episode Number" android:layout_weight="1.0" />
</LinearLayout>