2014-12-01 56 views
0

我的應用程序有一個問題,它包含一個MainActivity,它是一個ListActivity。應用程序以前工作得很好,但現在使用Android 5.0 Lollipop,它不會在ListView上顯示任何內容。它不會崩潰,視圖只是空的。調試告訴我,我的適配器有我想要顯示的數據。Android 5.0的ListActivity不填充ListView

佈局MainActivity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg" 
    tools:context=".MainActivity" > 

    <ImageView 
     android:id="@+id/bg_img" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_centerVertical="true" 
     android:contentDescription="@string/app_name" 
     android:scaleType="fitCenter" 
     android:src="@drawable/bg_image" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginBottom="10sp" 
     android:layout_marginEnd="10sp" 
     android:layout_marginLeft="10sp" 
     android:layout_marginRight="10sp" 
     android:layout_marginStart="10sp" 
     android:layout_marginTop="10sp" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="8dp" > 
    </ListView> 

</RelativeLayout> 

MainActivity:

public class MainActivity extends ListActivity { 
    private ArrayList<MyClass> class = new ArrayList<MyClass>(); 
    private ProgressDialog dialog; 
    private ArrayList<MyItem> classlist; 
    private Context context; 
    ListAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     classlist = new ArrayList<MyItem>(); 
     context = this; 

     if (contentInMemory) { 
      readFromMemory(); 
     } 
     if (!contentInMemory) { 
      readFromOnline() 
     } 
    } 

    private void readFromOnline() { 
     //read content from online 
     readOnline(); 
     doAfterLoad(); 
    } 

    private void readFromMemory() { 
     //read content from memory 
     doAfterLoad(); 
    } 

    private void doAfterLoad() { 
     adapter = new ListAdapter(context, class); 
     setListAdapter(adapter); 
    } 

    private void saveToMemory() { 
     //saves to memory 
    } 

    private class readOnline extends AsyncTask<String, String, String> { 

     protected void onPreExecute() { 
      //shows dialog 
     } 

     @Override 
     protected String doInBackground(String... strings) { 
      //reads content 
     } 

     @Override 
     protected void onPostExecute(String lenghtOfFile) { 
      saveToMemory(); 
      doAfterLoad(); 
     } 
    } 

    public class ListAdapter extends ArrayAdapter<MyClass>{ 
     private final Context context; 

     public ListAdapter(Context context, ArrayList<MyClass> class) { 
      super(context, R.layout.myclass_listlayout, class); 
      this.context = context; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      LayoutInflater inflater = (LayoutInflater) 
      context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      final View rowView = inflater.inflate(R.layout.myclass_listlayout, parent, false); 
      //fills textviews with content 
      return rowView; 
     } 
    } 
} 
+0

您從未在您的ListView上設置適配器。你也應該使用Activity,因爲你使用自己的佈局。 – 2014-12-01 22:30:31

+0

@JaredRummler我在'doAfterLoad()'中設置了適配器。無論如何,我將我的ListActivity更改爲活動,並試圖但不是,它不工作。我非常確定它會在Android 5.0以上工作(因爲我的代碼工作),但由於某些原因Lollipop已經改變了一些東西。 – mtlaa 2014-12-03 09:34:50

+0

發佈新代碼。你的'setListAdapter'將不起作用,因爲你正在使用你自己的佈局。您需要使用findViewById來查找ListView,然後使用'yourListView.setAdapter(adapter)'設置適配器' – 2014-12-03 09:38:21

回答

0

他應該如何膨脹的東西嗎?

你沒有覆蓋的

class Listadapter extends ArrayAdapter 

getCount() 

方法,告訴他適配器多少意見需要2充氣。沒有提供這些信息,適配器不會膨脹!你返回數組長度或列表大小getCount將

一些額外的提示

你解釋的問題是由在getCount將不返回一個數> 0的通病。

你可以簡單地通過增加

到您getView(..)方法檢查。

如果大部分時間沒有被調用,開發人員不會提供getCount()的有效返回值。

+0

將該檢查添加到'getView()'方法顯示它在多次調用時不會重寫'getCount()'正如它應該。添加該方法不會改變行爲。 – mtlaa 2014-12-03 09:14:36