0

当前有一个带有三个片段的 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

我能做些什么来解决这个问题?

我的主要活动是:

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

1 回答 1

1

由于 frags 是一个 Fragment 类型的数组,因此在将其分配给 frags 数组之前,请尝试将 bethoven 对象转换为 Fragment:

frags[0] = (Fragment) new bethoven();
于 2013-01-10T21:22:10.447 回答