在适配器和片段中获取正确的上下文

时间:2016-11-16 13:40:48

标签: java android android-layout fragment android-adapter

我的适配器出现错误

错误:(26,20)错误:不兼容的类型:MainFragment无法转换为Activity

我怀疑它是因为我试图使用的上下文类型是针对活动而不是片段

适配器

public class CustomListAdapter extends ArrayAdapter<String> {

   private final Activity context;
   private final String[] itemname;
   private final Integer[] imgid;

   public CustomListAdapter(MainFragment context, String[] itemname, Integer[] imgid) {
      super(context, R.layout.mylist, itemname);
      // TODO Auto-generated constructor stub
      this.context=context;
      this.itemname=itemname;
      this.imgid=imgid;
   }

   public View getView(int position,View view,ViewGroup parent) {
      LayoutInflater inflater=context.getLayoutInflater();
      View rowView=inflater.inflate(R.layout.mylist, null,true);

      TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
      ImageView imageView = (ImageView) rowView.findViewById(R.id.icons);
      TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

      txtTitle.setText(itemname[position]);
      imageView.setImageResource(imgid[position]);
      extratxt.setText("Description "+itemname[position]);
      return rowView;

   };
}

碎片

public class MainFragment extends Fragment {

    ListView list;
    String[] itemname ={
            "Safari",
            "Camera",
            "Global"
            /*"FireFox",
            "UC Browser",
            "Android Folder",
            "VLC Player",
            "Cold War"*/
    };

    Integer[] imgid={
            R.drawable.watersensoricon,
            R.drawable.ic_shutoff,
            R.drawable.ic_camera,

    };


    public MainFragment()
    {
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Bundle bundle = this.getArguments();
        //Integer data = EventBus.getDefault().removeStickyEvent(Integer.class);
        //if (data != null)
        //{
        //   classificationGroupFilter = data.intValue();
        //}

        // DashboardActivity activity = (DashboardActivity) getActivity();
        // int call = activity.openCallLogs();
        View view = inflater.inflate(R.layout.icons_main, container, false);
        //load(view);
        setupList(view);

        return view;
    }

//  @Override
private void setupList(View view){
        //super.onCreate(savedInstanceState);
        //setContentView(R.layout.icons_main);

        CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
        list=(ListView)view.findViewById(R.id.list);
    //(ListView)view.findViewById(R.id.lv_listview);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                // TODO Auto-generated method stub
                String Slecteditem= itemname[+position];
                //Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();

            }
        });
    }
}

XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context ="com.xera.deviceinsight.home.MainFragment">
          <!--      tools:context="{relativePackage}.${activityClass}" >-->

   <ListView
      android:id="@+id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true" >
   </ListView>

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

适配器需要context的{​​{1}}。您可以使用片段的getActivity()方法来访问Activity上下文。请注意,您需要确保Activity附加fragment

所以要修复此替换

Activity

CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);

答案 1 :(得分:0)

更改

CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);

CustomListAdapter adapter=new CustomListAdapter(getActivity(), itemname, imgid);

因为在您的情况下,this链接到MainFragment,而不是活动。

答案 2 :(得分:0)

你可以使用

viewGroup.getContext()

在onCreateViewHolder中,或

viewHolder.getView().getContext()

onBindViewHolder中的