我的應用程序有一個問題,它包含一個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;
}
}
}
您從未在您的ListView上設置適配器。你也應該使用Activity,因爲你使用自己的佈局。 – 2014-12-01 22:30:31
@JaredRummler我在'doAfterLoad()'中設置了適配器。無論如何,我將我的ListActivity更改爲活動,並試圖但不是,它不工作。我非常確定它會在Android 5.0以上工作(因爲我的代碼工作),但由於某些原因Lollipop已經改變了一些東西。 – mtlaa 2014-12-03 09:34:50
發佈新代碼。你的'setListAdapter'將不起作用,因爲你正在使用你自己的佈局。您需要使用findViewById來查找ListView,然後使用'yourListView.setAdapter(adapter)'設置適配器' – 2014-12-03 09:38:21