我想在我的列表視圖中顯示一個按鈕,以防萬一我沒有數據通過自定義數組適配器填充列表。如果我有數據和我的列表視圖獲取數據項填充,但如果我沒有數據項目顯示列表視圖應該顯示LinearLayout ID爲「@android:ID /空」,但它從來沒有顯示這個和我的應用程序在這個階段崩潰。在我的FavoriteStudents.java類文件我有一個AsyncTask defined.In。 doInBackground方法我檢查我的手機內部存儲器中是否存在最喜歡的文件以創建用於listview的適配器,如果沒有,則停止AsyncTask並顯示帶有buttom的列表視圖。從doInBackground方法返回後,它崩潰並向我顯示此錯誤:如何在列表視圖中顯示emptyview以防萬一我的適配器爲空?
10-16 16:06:37.568: E/WindowManager(274): Activity com.example.hellogridview.FavoriteStudets has leaked window [email protected] that was originally added here
10-16 16:06:37.568: E/WindowManager(274): android.view.WindowLeaked: Activity com.example.hellogridview.FavoriteStudents has leaked window [email protected] that was originally added here
10-16 16:06:37.568: E/WindowManager(274): at android.view.ViewRoot.<init>(ViewRoot.java:227)
10-16 16:06:37.568: E/WindowManager(274): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-16 16:06:37.568: E/WindowManager(274): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-16 16:06:37.568: E/WindowManager(274): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-16 16:06:37.568: E/WindowManager(274): at android.app.Dialog.show(Dialog.java:239)
10-16 16:06:37.568: E/WindowManager(274): at com.example.hellogridview.FavoriteStudents$readingFavFileTask.onPreExecute(FavoriteStudents.java:50)
10-16 16:06:37.568: E/WindowManager(274): at android.os.AsyncTask.execute(AsyncTask.java:391)
10-16 16:06:37.568: E/WindowManager(274): at com.example.hellogridview.FavoriteStudents.onCreate(FavoriteStudents.java:34)
10-16 16:06:37.568: E/WindowManager(274): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-16 16:06:37.568: E/WindowManager(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-16 16:06:37.568: E/WindowManager(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-16 16:06:37.568: E/WindowManager(274): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-16 16:06:37.568: E/WindowManager(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-16 16:06:37.568: E/WindowManager(274): at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 16:06:37.568: E/WindowManager(274): at android.os.Looper.loop(Looper.java:123)
10-16 16:06:37.568: E/WindowManager(274): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-16 16:06:37.568: E/WindowManager(274): at java.lang.reflect.Method.invokeNative(Native Method)
10-16 16:06:37.568: E/WindowManager(274): at java.lang.reflect.Method.invoke(Method.java:521)
10-16 16:06:37.568: E/WindowManager(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-16 16:06:37.568: E/WindowManager(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-16 16:06:37.568: E/WindowManager(274): at dalvik.system.NativeStart.main(Native Method)
我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/studentsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/student_list_selector" >
</ListView>
<!-- empty view -->
<LinearLayout
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/addStudents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Students" >
</Button>
</LinearLayout>
</LinearLayout>
我的學生適配器
public class StudentsAdapter extends ArrayAdapter<Students> {
private Context context;
ImageDownloaderNew imageDownloader;
ArrayList<Students> students;
Songs studentsTemp = null;
int targetWidth = 70;
int targetHeight = 110;
public StudentsAdapter (Context context, int textViewResourceId,
ArrayList<Students> students) {
super(context, textViewResourceId, songs);
// TODO Auto-generated constructor stub
this.context = context;
this.students= students;
imageDownloader = new ImageDownloaderNew();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return students.size();
}
@Override
public Students getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
static class ViewHolder {
protected TextView studentTitle;
protected RatingBar studentRating;
protected TextView studentViews;
protected ImageView studentImage;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//My getView code goes here.I have not pasted here
return convertView;
}
}
現在FavoriteStudents.java類文件
public class FavoriteStudents extends SherlockActivity {
ListView list;
StudentsAdapter adapter;
ArrayList<Students> studentsListArray = new ArrayList<Students>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.students_list);
new readingFavFileTask().execute(UtilityFuctions.FAV_STUDENTS_FILE_NAME);
}
private class readingFavFileTask extends AsyncTask<String, Void, String> {
ProgressDialog m_dialog = new ProgressDialog(FavoriteStudents.this);
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// initialize the dialog
m_dialog.setTitle("Loading...");
m_dialog.setMessage("Please wait while loading...");
m_dialog.setIndeterminate(true);
m_dialog.setCancelable(true);
m_dialog.show();
}
@Override
protected String doInBackground(String... params) {
if (!UtilityFuctions.isFileExists(getApplicationContext(),
params[0])) {
// favorite file does not exists
finish();
return "fileDoesNotExists";
}else{
StudentsListArray = UtilityFuctions.readFavJSONFile(
getApplicationContext(), params[0]);
if (isCancelled())
finish();
return null;}
}
@Override
protected void onPostExecute(String result) {
list = (ListView) findViewById(R.id.studentsList);
if (result.equals("fileDoesNotExists")) {
adapter = new StudentsAdapter(FavoriteStudents.this, 1,
studentsListArray);
list.setAdapter(adapter);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.empty_view, null);
list.setEmptyView(myView);
m_dialog.dismiss();
} else {
// Creating a StudentsAdapter and setting it on listview
adapter = new StudentsAdapter(FavoriteStudents.this, 1,
studentsListArray);
list.setAdapter(adapter);
m_dialog.dismiss();
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//onItemClick code goes here
}
});
}
}
}
}
編輯:@Rol f Smith的答案是正確的。但主要問題是我在我的doInBackground()方法中調用了finish(),它正在殺死我的活動,並沒有向我顯示emptyView。因此,我只是刪除了finish()和一切正常工作。 Thanks EveryOne
修改您的適配器的getView,以便在沒有數據的情況下使用按鈕返回線性佈局?或者使用ListView.setEmptyView() – Shark
好的我正在嘗試ListView.setEmptyView() – Anshul
感謝@Shark的幫助 – Anshul