有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在片段类中访问setAdapter?

我有一个Fragment类:

public class TabFragment extends Fragment {
  ....
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   {
     View view =  inflater.inflate(R.layout.fragment_photos_tab, container, false);
     TabelAdapter adapt = new TabelAdapter(getActivity().getApplicationContext(), R.layout.list_room_view, flowerList);
     setListAdapter(adapt);     // Not Working because class is not extended by ListActivity
     return view;
   }

TabelAdapter类

public class TabelAdapter extends ArrayAdapter<Flower> {

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
   if (null == convertView) {
        convertView = inflater.inflate(R.layout.list_flower_view, parent, false);
    }
    Picasso
            .with(context)
            .load("http://i.imgur.com/rFLNqWI.jpg")
            .fit()
            .into((ImageView) convertView);
    return convertView;
}

列表\u花卉\u视图。xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/Flowerimg"
    安卓:layout_width="match_parent"
    安卓:layout_height="200dp"/>

你回家了。xml

 <RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent" tools:context=".MainActivity">

<ListView
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:id="@安卓:id/list"></ListView>
</RelativeLayout>

当我使用activity而不是fragment时,整个过程非常简单。我用ListActivity扩展了MainActivity类,并将setContentView(R.layout.activity_home);添加到MainActivity.OnCreate()。我可以在ListView中看到图像列表

问题:

如何使setListAdapterFragment类中可用?我是^{的新手。任何帮助都将是可观的:)

编辑-1

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            TabFragment photo = new TabFragment();
            return photo;  // Line (30,24)
  }        ......
 }

如果我使用ListFragment而不是Fragment,那么PagerAdapter类操作系统将抛出一个错误

Error:(30, 24) error: incompatible types: TabFragment cannot be converted to Fragment

编辑-2

java.lang.RuntimeException: Your content must have a ListView whose id attribute is '安卓.R.id.list'

共 (1) 个答案

  1. # 1 楼答案

    Fragment更改为ListFragment

     public class TabFragment extends ListFragment {
    
    @Override
          public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            TabelAdapter adapt = new TabelAdapter(getActivity().getApplicationContext(), R.layout.list_room_view, flowerList);
            setListAdapter(adapt);
           }
         ...
        }
    

    ListFragment documentation

    Tutorial