更新Fragment中setUserVisibilityHint()方法中的列表不起作用

时间:2014-06-09 10:32:21

标签: android database performance sqlite android-fragments

我正在使用ViewPager&每次尝试更新列表视图通过覆盖我的listFragment中的setUserVisibilityHint()方法[我的代码中的第二个最后一个方法]向用户显示ListFragment,但代码不起作用。 以下是List Fragment代码的片段:

public void showShoppinglist() {
    String[] columns = new String[] { testshop.TABLE_ROW_ID,
            testshop.TABLE_ROW_ONE, testshop.TABLE_ROW_TWO };
    Cursor cursor = testshop.db.query(testshop.TABLE_NAME, columns, null, null,
            null, null, null);
      listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
    cursor.moveToFirst();//move the cursor to the first item
    if (!cursor.isAfterLast()) {
        do {
            prepareListData(cursor);


        } while (cursor.moveToNext());
    }
    //cursor.close(); // closing of the cursor
     // closing the database
}
/*
 * Preparing the list data
 */



public void prepareListData(final Cursor items) {


    // Adding child data
    listDataHeader.add(items.getString(1));


    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("Born Sinner");

    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data

}

@Override
public void setUserVisibleHint(boolean isVisibleToUser){//saved my life
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisibleToUser){
        testshop.db = testshop.helper.getWritableDatabase();

             //fetch a cursor and update the listView from it   
             showShoppinglist();// this line doesn't work, List isnt't being
                                 // updated


        Log.d(getTag(), "WORKS LIKE A CHARM");//this line works
        }
    else{}
}
@Override
public void onResume(){
    super.onResume();
    showShoppinglist();
    }

   } 

tabadapter代码:

package com.fragmment.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class tabAdapter extends FragmentPagerAdapter {

public tabAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // add fragment activity
        return new addFragment();
    case 1:
        // list fragment activity
        return new ListFragment();
    case 2:
        //Alert fragment
        return new AlertFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}


}

0 个答案:

没有答案