Android 填充片段列表时出现问题

Android 填充片段列表时出现问题,android,android-listfragment,Android,Android Listfragment,我有一个SherlockFragmentActivity类,它从服务器收集值并将其加载到我的数据库中。这个夏洛克的片段被称为书、视频和音频的3个片段。它们中的每一个都用来显示下载到数据库中的值。现在的挑战是,当我打开我的UI时,我不会看到片段上的值,直到我开始单击每个片段,然后才将值填充到片段中的列表中。我甚至注意到这个值的不断增加。我的片段类粘贴在下面 public class BooksFragment extends SherlockListFragment{ TextV

我有一个SherlockFragmentActivity类,它从服务器收集值并将其加载到我的数据库中。这个夏洛克的片段被称为书、视频和音频的3个片段。它们中的每一个都用来显示下载到数据库中的值。现在的挑战是,当我打开我的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);
         }


}
public类BooksFragment扩展了SherlockListFragment{
TextView TextView=null;
字符串CategoryID=null;
ArrayList listBooks=null;
IDatabaseHelper databaseHelper=null;
活动=空;
Context=null;
ListAdapter=null;
公共图书馆({
超级();
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.books,container,false);
//你看这里吗
textview=(textview)view.findViewById(R.id.textView1);
返回视图;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
listBooks=newarraylist();
}
@凌驾
public void onStart(){
super.onStart();
Bundle=this.getArguments();
if(bundle!=null){
CategoryID=bundle.getString(“CategoryID”);
}
this.initializeComponents();
this.populateListView();
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
activity=getActivity();
context=activity.getBaseContext();
databaseHelper=新的databaseHelper(上下文);
}
//现在我们将初始化片段的组件
私有void initializeComponents(){
ListView ListView=getListView();
setOnItemClickListener(listener);
}
//列表项单击侦听器
私有OnItemClickListener侦听器=新建OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//TODO自动生成的方法存根
}
};
//此方法将用于从数据库收集内容并填充listview项
私有void populateListView(){
MedicalBookModel[]BookModel=this.databaseHelper.ReturnBooks(CategoryID);
if(booksmodel!=null){
for(MedicalBook模型书:BookModel){
HashMap bookMap=新建HashMap();
bookMap.put(MedicalBookModel.MedicalBookModel_ID,book.getID());
bookMap.put(MedicalBookModel.MedicalBookModel_Name,book.getName());
Log.i(“值”,book.getName());
listBooks.add(bookMap);
}
}
adapter=newsimpledapter(上下文,listBooks,R.layout.list_book,新字符串[]{“ID”,“Name”},新int[]{R.ID.bookId,R.ID.bookName});
setListAdapter(适配器);
}
}

为此,您有几种解决方案:

1-使用全局的应用程序实例单例

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

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

4-添加片段时,在参数中将对象作为可分块传递

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

可分包对象示例

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 });
    }
}

A不要使用listFragment,因此从我的观点来看,我建议您使用带有BaseAdapterThanks的ListView片段作为您的响应。我真的很棒。我会让你知道事情的进展,还是一样。我试过一个普通的片段和同一个故事。我认为这与何时调用值以添加到片段列表有关。是否有一种方法可以从我的活动中加载片段中的列表。这样,一旦加载了活动。我想碎片很容易填充。我有办法做到这一点。我想从活动中收集要填充到片段中的值。所以我现在在创建片段时传递这个值。但我正在以ArrayList的形式加载我的列表。我不知道如何将此项目作为一个包。我已经能够将其传递到我的片段中。现在我正试图用它来填充我的列表。谢谢,我想我会喜欢第四步的。但是我尝试了传递值,我没有看到允许我在Bundle对象中传递ArrayList的方法。你知道如何传递它吗?如果你尝试“intent.putParcelableArrayListExtra(“key”,ArrayList)”;“Bundle似乎不必使用上面的方法。当向片段传递值时,我们使用bundle.bundle.putParcelableArrayList(“,booksmodel”);我一直在尝试,但现在我有错误。我在这里很困惑,不知道如何将ArrayList传递到包中。