2012-04-21 93 views
0

试图使其工作正常......它加载正常,甚至会告诉应用程序已完成获取所有数据。它不会填充列表视图。无法使用AsyncTask填充ListView

内部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> 
+0

它给你你的alertdialog? – JRaymond 2012-04-21 22:11:58

+0

警报对话框显示“获取数据” - 我为其设置的字符串。当它完成时,alertdialog实际上被解雇了,没有错误。 – 2012-04-21 22:20:57

+0

你在logcat中看到这个日志输出吗? (Log.d(DEBUG_TAG,mArrayList.toString());) – 2012-04-21 23:24:58

回答

1

不要list_row_show.xml布局的根元素的layout_height设置FILL_PARENT。