how can i update view when fragment change?

馋奶兔 提交于 2020-01-22 02:19:12

问题


i have a activity that have 2 sherlockfragment in this The first two pages display fragments with custom list views which are built from xml from server using AsyncTask. However, when the app runs, only one list view is displayed, the other page is just blank

public class VpiAbsTestActivity extends SherlockFragmentActivity {


private static final String[] CONTENT = new String[] { "1","2"};

TestFragmentAdapter mAdapter;
 ViewPager mPager;
    PageIndicator mIndicator;



protected void onCreate(Bundle savedInstanceState) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_tabs);

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
    mIndicator.setViewPager(mPager);
    mIndicator.notifyDataSetChanged();
}



class TestFragmentAdapter extends FragmentPagerAdapter {        
    private int mCount = CONTENT.length;

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

    @Override
    public Fragment getItem(int position) {
        switch(position) {

        case 0:
            return new customlist();


        case 1:
            return new customlistnotuser();


        default:
            return null;

        }
    }

    @Override
    public int getCount() {
        return mCount;
    }


    public CharSequence getPageTitle(int position) {
        return VpiAbsTestActivity.CONTENT[position % VpiAbsTestActivity.CONTENT.length].toUpperCase();
    }

     @Override
        public void destroyItem(View collection, int position, Object view) {
             ((ViewPager) collection).removeView((View) view);
        }

 }

}

what can i update viewpager when change pages ?

the customlistnotuser page likes customlist page but not show

 public class customlistnotuser extends SherlockFragment  {

// All static variables
static final String URL = "url";
// XML node keys
static final String KEY_TEST = "test"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_Description = "description";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_PRICE = "price";
static final String KEY_URL = "url";



 private ProgressDialog pDialog;


ListView list;
LazyAdapterbeth adapter;
XMLParser parser = new XMLParser();
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


}



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

    new getFeed().execute();

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 

{

    View thisfragment = inflater.inflate(R.layout.dovomi, container, false);

    return thisfragment;
}



private class getFeed extends AsyncTask<Void, Void, Document> {



       }

    protected Document doInBackground(Void... params) {

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        return doc;
    }

    protected void onPostExecute(Document doc) {

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

    NodeList nl = doc.getElementsByTagName(KEY_TEST);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_Description, parser.getValue(e, KEY_Description));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
        map.put(KEY_PRICE, parser.getValue(e, KEY_PRICE));
        map.put(KEY_URL, parser.getValue(e, KEY_URL));
        // adding HashList to ArrayList
        songsList.add(map);
         pDialog.dismiss();
    }



    list=(ListView)getActivity().findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapterbeth(getActivity(), songsList);
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

the customlistnotuser page likes customlist page but not show .. :(

来源:https://stackoverflow.com/questions/13687280/how-can-i-update-view-when-fragment-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!