填充片段列表的问题

时间:2013-10-02 11:01:38

标签: android android-listfragment

我有一个SherlockFragmentActivity类,它从服务器收集值并将其加载到我的数据库中。这个SherlockFragmentActivity为3片段称为Book,Video和Audios。它们中的每一个都是为了显示下载到数据库中的值。现在挑战是当我打开我的UI时,我不会看到片段上的值,直到我开始点击每个片段,然后将值填充到片段中的列表中。我甚至注意到这些价值的不断增加。我的片段类粘贴在下面。

    public class BooksFragment extends SherlockListFragment{

    TextView textview = null;
    String CategoryID = null;

    ArrayList<HashMap<String,String>> listBooks = null;

    IDatabaseHelper databaseHelper = null;

    Activity activity = null;
    Context context = null;

    ListAdapter adapter = null;


    public BooksFragment(){

        super();
    }


     @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.books, container, false);
        // do your view initialization heres
        textview = (TextView)view.findViewById(R.id.textView1);



        return view;
      }



     @Override
     public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);

         listBooks = new ArrayList<HashMap<String,String>>();

     }


      @Override
        public void onStart() {
            super.onStart();


            Bundle bundle =this.getArguments();

            if(bundle != null){
                CategoryID = bundle.getString("CategoryID");           
            }

            this.initializeComponents();
            this.populateListView();
      }


      @Override
      public void onActivityCreated(Bundle savedInstanceState){
          super.onActivityCreated(savedInstanceState);

          activity = getActivity();
          context = activity.getBaseContext();

          databaseHelper= new DatabaseHelper(context);


      }


      //Now we are going to initialize components of the fragment
      private void initializeComponents(){

          ListView listview = getListView();
          listview.setOnItemClickListener(listener);
      }


      //list item click listener
        private OnItemClickListener listener = new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub

            }


         };


         //This method would be used to collect content from the database and populate the listview item
         private void populateListView(){
             MedicalBookModel[] booksmodel = this.databaseHelper.ReturnBooks(CategoryID);

             if(booksmodel != null){
                 for(MedicalBookModel book : booksmodel){

                     HashMap<String,String> bookMap = new HashMap<String,String>();
                     bookMap.put(MedicalBookModel.MedicalBookModel_ID, book.getID());
                     bookMap.put(MedicalBookModel.MedicalBookModel_Name,book.getName());

                     Log.i("values",book.getName());

                     listBooks.add(bookMap);
                 }
             }

             adapter = new SimpleAdapter(context, listBooks,R.layout.list_book,new String[]{ "ID","Name"}, new int[]{ R.id.bookId, R.id.bookName});
             setListAdapter(adapter);
         }


}

1 个答案:

答案 0 :(得分:1)

为此你有几个解决方案:

1 - 使用全局

的Application实例单例

2 - 创建自己的全局类来管理数据

3 - 使用绑定到活动(或不绑定)的服务和回叫(可能是意图和广播接收者)

4 - 在添加片段

时,将对象作为参数传递给parceable

请注意,有时您需要使视图无效以强制数据刷新

可怜对象的豁免

public class ImageObject implements Parcelable {
    /**
     * ATTRIBUTES
     */
    protected String    _idPicture;
    protected String    _idAlbum;
    protected String    _name;
    protected String    _fileName;
    protected String    _imageUrl;
    protected String    _hierarchy;

    public ImageObject(String _idPicture, String _idAlbum, String _name, String _fileName, String _imageUrl, String _hierarchy) {

        super();
        this._idPicture = _idPicture;
        this._idAlbum = _idAlbum;
        this._name = _name;
        this._fileName = _fileName;
        this._imageUrl = _imageUrl;
        this._hierarchy = _hierarchy;
    }

    public ImageObject(Parcel in) {

        String[] data = new String[6];
        in.readStringArray(data);
        this._idPicture = data[0];
        this._idAlbum = data[1];
        this._name = data[2];
        this._fileName = data[3];
        this._imageUrl = data[4];
        this._hierarchy = data[5];
    }

    public static final Parcelable.Creator  CREATOR = new Parcelable.Creator() {
                                                        public ImageObject createFromParcel(Parcel in) {

                                                            return new ImageObject(in);
                                                        }

                                                        public ImageObject[] newArray(int size) {

                                                            return new ImageObject[size];
                                                        }
                                                    };

    @Override
    public int describeContents() {

        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeStringArray(new String[] { this._idPicture, this._idAlbum, this._name, this._fileName, this._imageUrl, this._hierarchy });
    }
}