分享

The content of the adapter has changed but ListView did not receive

 android之情殇 2014-03-18
 同样问题 请教高手:


public class CatalogActivity extends Activity {

        private ImageView back;

        private ListView listView;
        private List<CatalogModel> data;
        private CatalogAdapter adapter;

        private AsyncCatalog async = null;
        private ProgressDialog progressDialog = null;
        private String path = "index.txt";

         private SharedPreferences sharedPreferences=null;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.catalog);

                 sharedPreferences = CatalogActivity.this.getSharedPreferences(
                 "LYQB", MODE_PRIVATE);

                listView = (ListView) findViewById(R.id.catolog_listView);
                data = new ArrayList<CatalogModel>();
                adapter = new CatalogAdapter(this, data);
                listView.setAdapter(adapter);

                async = new AsyncCatalog();
                async.execute(path);

                listView.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                                CatalogModel model = (CatalogModel) parent
                                                .getItemAtPosition(position);
                                String title = model.getTitle();

                                 SharedPreferences.Editor editor = sharedPreferences.edit();
                                 editor.putInt(title, 1);
                                 editor.commit();

                                Intent intent = new Intent(CatalogActivity.this,
                                                ReadingActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("title", title);
                                bundle.putString("path", position + ".txt");
                                intent.putExtras(bundle);
                                CatalogActivity.this.startActivity(intent);

                        }

                });
                back = (ImageView) findViewById(R.id.catolog_back);
                back.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                CatalogActivity.this.finish();
                        }
                });
        }

        private void getIndex(String index) {
                String file = "book/" + index;
                AssetManager am = null;

                InputStream is = null;
                BufferedReader reader = null;
                String line = null;
                CatalogModel model = null;
                try {
                        am = this.getAssets();
                        is = am.open(file);
                        Log.v("", ">>>>>>>>>>>>>>>>>>>>>>>>>>is.available(): " + is.available());
                        reader = new BufferedReader(new InputStreamReader(is));

                        while ((line = reader.readLine()) != null) {
                                Log.v("", ">>>>>>>>>>>>>>>>>>>>>>>>>>.....line: " + line);
                                model = new CatalogModel();
                                model.setTitle(line);
                                // model.setIsSaw(sharedPreferences.getInt(line, 0));
                                data.add(model);
                        }
                        is.close();
                        reader.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }

          class AsyncCatalog extends AsyncTask<String, Void, Void> {

                @Override
                protected void onPreExecute() {
//                        progressDialog = ProgressDialog.show(CatalogActivity.this, "",
//                                        "loading.....", true);
                        super.onPreExecute();
                }

                @Override
                protected Void doInBackground(String... params) {
                        String index = params[0];
                        getIndex(index);
                        return null;
                }

                @Override
                protected void onPostExecute(Void result) {
//                        progressDialog.dismiss();
                        adapter.notifyDataSetChanged();
                        super.onPostExecute(result);
                }
        }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多