将listview片段添加到viewpager中

时间:2012-10-28 18:50:45

标签: android listview android-viewpager fragment

目前有一个ViewPager,其中有三个片段作为我要为此

添加自定义列表视图的页面

我的列表代码片段是:

public class bethoven extends Fragment  {
// All static variables
static final String URL = "http://test.com/bethhoven1.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_URL = "url";

ListView list1;
LazyAdapterbeth adapter;
XMLParser parser1 = new XMLParser();

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    new getFeed().execute();
}


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

}

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

{
    View thisfragment1 = inflater.inflate(R.layout.fragment_layout, container, false);

    return thisfragment1;
}

private class getFeed extends AsyncTask<Void, Void, Document> {
    @Override
    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_SONG);
    // 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, parser1.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser1.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser1.getValue(e, KEY_ARTIST));
        map.put(KEY_DURATION, parser1.getValue(e, KEY_DURATION));
        map.put(KEY_THUMB_URL, parser1.getValue(e, KEY_THUMB_URL));
        map.put(KEY_URL, parser1.getValue(e, KEY_URL));
        // adding HashList to ArrayList
        songsList.add(map);
    }

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

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


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

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


         }
    }); 
}

}}

我的标题适配器编号是:

public class TitleAdapter extends FragmentPagerAdapter{
private String titles[] = new String[]{"View1","View2","View3"};
private Fragment frags[] = new Fragment[titles.length]; 

public TitleAdapter(FragmentManager fm) {
    super(fm);
    frags[0] = new bethoven();
    frags[1] = new FragmentView2();
    frags[2] = new FragmentView3();

}


@Override
public CharSequence getPageTitle (int position){
    Log.v("TitleAdapter - getPageTitle=", titles[position]);
    return titles[position];
}

@Override
public Fragment getItem(int position) {
    Log.v("TitleAdapter - getItem=", String.valueOf(position));
    return frags[position];
}

@Override
public int getCount() {
    return frags.length;
}

但是当我添加frags [0] = new bethoven();出现错误

类型不匹配:无法从bethoven转换为Fragment

我该怎么做才能解决这个问题?

我的mainActivity是:

public class MainActivity extends FragmentActivity {
ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagerTabStrip);
    //pagerTabStrip.setTextColor(color.white);
    //pagerTabStrip.setTabIndicatorColor(color.holo_red_dark);


    TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(titleAdapter);
    mViewPager.setCurrentItem(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

1 个答案:

答案 0 :(得分:1)

由于frags是Fragment类型的数组,在将它分配给frags数组之前,尝试将bethoven对象转换为Fragment:

frags[0] = (Fragment) new bethoven();