【问题标题】:ListView continuously recreating upon Activity changes.ListView 在 Activity 更改时不断重新创建。
【发布时间】:2014-05-08 00:40:12
【问题描述】:

我有这个ListView;但是当我离开或返回Activity 时,ListView 会不断自我重建。是什么原因造成的,我应该在下面的代码中寻找什么

public class Homepage extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);
        ImageView i = (ImageView) findViewById(R.id.imageView1);
        Ion.with(i).placeholder(R.drawable.ic_launcher)
                .error(R.drawable.ic_launcher)
                .load("http://192.168.1.6/webservice/images/image1.jpg");

        // Creating a new non-ui thread task to download json data
        DownloadTask downloadTask = new DownloadTask();
        // Starting the download process
        downloadTask.execute(strUrl);

        // Getting a reference to ListView of activity_main
        mListView = (ListView) findViewById(R.id.myList);
        startService(new Intent(this, NotificationService.class));

    }
    ....

    String strUrl = "http://192.168.1.6/webservice/events.php";
    ListView mListView;

    /** A method to download json data from url */
    private String downloadUrl(String strUrl) throws IOException {
        String data = "";
        InputStream iStream = null;
        try {
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            HttpURLConnection urlConnection = (HttpURLConnection) url
                    .openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    iStream));

            StringBuffer sb = new StringBuffer();

            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            data = sb.toString();

            br.close();

        } catch (Exception e) {
            Log.d("Exception while downloading url", e.toString());
        } finally {
            iStream.close();
        }

        return data;
    }

    /** AsyncTask to download json data */
    private class DownloadTask extends AsyncTask<String, Integer, String> {
        String data = null;

        @Override
        protected String doInBackground(String... url) {
            try {
                data = downloadUrl(url[0]);

            } catch (Exception e) {
                Log.d("Background Task", e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {

            // The parsing of the xml data is done in a non-ui thread
            ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();

            // Start parsing xml data
            listViewLoaderTask.execute(result);

        }
    }

    /** AsyncTask to parse json data and load ListView */
    private class ListViewLoaderTask extends
            AsyncTask<String, Void, SimpleAdapter> {

        JSONObject jObject;

        // Doing the parsing of xml data in a non-ui thread
        @Override
        protected SimpleAdapter doInBackground(String... strJson) {
            try {
                jObject = new JSONObject(strJson[0]);
                JSONParser newJsonParser = new JSONParser();
                newJsonParser.parse(jObject);
            } catch (Exception e) {
                Log.d("JSON Exception1", e.toString());
            }

            // Instantiating json parser class
            JSONParser newJsonParser = new JSONParser();

            // A list object to store the parsed events list
            List<HashMap<String, Object>> posts = null;

            try {
                // Getting the parsed data as a List construct
                posts = newJsonParser.parse(jObject);
            } catch (Exception e) {
                Log.d("Exception", e.toString());
            }

            // Keys used in Hashmap
            String[] from = { "mytitle", "event_img", "mymessage", "mysponser",
                    "myevent_location", "myevent_whoinvited",
                    "myevent_dresscode", "myevent_time", "myevent_endtime" };

            // Ids of views in listview_layout
            int[] to = { R.id.title, R.id.event_pic, R.id.subTitle_single,
                    R.id.sponser, R.id.l, R.id.who, R.id.d, R.id.t, R.id.e };

            // Instantiating an adapter to store each items
            // R.layout.listview_layout defines the layout of each item
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), posts,
                    R.layout.single_line, from, to);
            return adapter;
        }

        ....
        /** Invoked by the Android on "doInBackground" is executed */
        @Override
        protected void onPostExecute(final SimpleAdapter adapter) {

            // Setting adapter for the listview
            mListView.setAdapter(adapter);
            // //Attempting Onclick method \\\\
            mListView.setOnItemClickListener(new OnItemClickListener() {

                ....
            });

            // //\\\\looks like this cycles through the image to the adapter
            // using the ImageLoaderTask
            for (int i = 0; i < adapter.getCount(); i++) {
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter
                        .getItem(i);
                String imgUrl = (String) hm.get("event_img_path");
                ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

                HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("event_img_path", imgUrl);
                hm.put("position", i);

                // Starting ImageLoaderTask to download and populate image in
                // the listview
                imageLoaderTask.execute(hm);
            }
        }
    }

    /** AsyncTask to download and load an image in ListView */
    private class ImageLoaderTask extends
            AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>> {

        @Override
        protected HashMap<String, Object> doInBackground(
                HashMap<String, Object>... hm) {

            InputStream iStream = null;
            String imgUrl = (String) hm[0].get("event_img_path");
            int position = (Integer) hm[0].get("position");

            URL url;
            try {
                url = new URL(imgUrl);

                // Creating an http connection to communicate with url
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();

                // Connecting to url
                urlConnection.connect();

                // Reading data from url
                iStream = urlConnection.getInputStream();

                // Getting Caching directory
                File cacheDirectory = getBaseContext().getCacheDir();

                // Temporary file to store the downloaded image
                File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"
                        + position + ".png");

                // The FileOutputStream to the temporary file
                FileOutputStream fOutStream = new FileOutputStream(tmpFile);

                // Creating a bitmap from the downloaded inputstream
                Bitmap b = BitmapFactory.decodeStream(iStream);

                // Writing the bitmap to the temporary file as png file
                b.compress(Bitmap.CompressFormat.PNG, 100, fOutStream);

                // Flush the FileOutputStream
                fOutStream.flush();

                // Close the FileOutputStream
                fOutStream.close();

                // Create a hashmap object to store image path and its position
                // in the listview
                HashMap<String, Object> hmBitmap = new HashMap<String, Object>();

                // Storing the path to the temporary image file
                hmBitmap.put("event_img", tmpFile.getPath());

                // Storing the position of the image in the listview
                hmBitmap.put("position", position);

                // Returning the HashMap object containing the image path and
                // position
                return hmBitmap;

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(HashMap<String, Object> result) {
            // Getting the path to the downloaded image
            String path = (String) result.get("event_img");

            // Getting the position of the downloaded image
            int position = (Integer) result.get("position");

            // Getting adapter of the listview
            SimpleAdapter adapter = (SimpleAdapter) mListView.getAdapter();

            // Getting the hashmap object at the specified position of the
            // listview
            // @SuppressWarnings("unchecked")
            HashMap<String, Object> hm = (HashMap<String, Object>) adapter
                    .getItem(position);

            // Overwriting the existing path in the adapter
            hm.put("event_img", path);

            // Noticing listview about the dataset changes
            adapter.notifyDataSetChanged();
        }
    }

}

我已经将android:configChanges="orientation" 添加到AndroidManifest.xml 文件中,但这并没有解决问题。

【问题讨论】:

  • 对于 API LEVEL > 11 的设备,它应该是 android:configChanges="orientation|screenSize"。仍然不是正确的管理方法
  • 如果可行的话,那只会解决方向问题。但是当活动重新成为焦点时,列表视图仍将重新创建。有人告诉我它与 asynctask 有关,但这超出了我的范围。
  • 因为 onCreate() 在那里你有 asytask 并且每次当屏幕改变时它都会进入 onCreate 并且它会再次调用 asyntask。尝试用布尔值阻止它,或者如果我没记错的话,屏幕更改时不会调用 onCreate 的配置。
  • 我看到一个布尔值。如果我在 imageloadertask 中放一个布尔值来检查图像是否已经存储在缓存中怎么办?

标签: android listview arraylist android-activity


【解决方案1】:

ListView 将始终被销毁,因为它与您的 Activity 的生命周期相关联。一旦Activity 死了,ListView 也会死。为了避免这种情况;设置某种数据存储/检索,将存储您需要的 ArrayList 信息并在 Activity 更改时重新创建它。

您还添加了相当多的代码;将事情简明扼要地简化为您认为导致问题和相关类的原因。

创建一个新的类文件并将以下代码放入:

    public class EagerSingleton {
    private ArrayList<MyClass> list = new ArrayList<MyClass>();;
    private static volatile EagerSingleton instance = null;

    // private constructor
    private EagerSingleton() {
    }

    public static EagerSingleton getInstance() {
        if (instance == null) {
            synchronized (EagerSingleton.class) {
                // Double check
                if (instance == null) {
                    instance = new EagerSingleton();
                }
            }
        }
        return instance;
    }
}

然后你可以为ArrayList参数添加getter和setter方法。

这意味着无论您在何处实例化 Singleton,您都可以访问该数组列表。

【讨论】:

  • 我从我的帖子中删除了一些代码。我认为问题在于 downloadtask();每次创建活动时都在运行。但我不确定,我将尝试以某种方式做出一个 if 语句来检查数据是否已经下载......
  • 您的列表视图也在重新创建;但只是空的?还是填充数据?
  • 它每次都在更新数据,我想完全控制它何时刷新。
  • 尝试将该数据存储在另一个类中;也许是一个单例对象;并从单例对象中检索数据?这样,它是否重新创建列表视图就无关紧要
  • 哇,对不起,请记住,您在这里是在和一个新手说话,哈哈,我还在学习过程中,你能把它分解一下吗?什么是单例对象?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 2022-10-19
  • 1970-01-01
  • 2017-11-16
相关资源
最近更新 更多