gpt4 book ai didi

java - 在适配器和 fragment 中获取正确的上下文

转载 作者:行者123 更新时间:2023-12-01 09:14:50 25 4
gpt4 key购买 nike

我的适配器出现错误

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

我怀疑这是因为我尝试使用的上下文类型是用于 Activity 而不是 fragment

适配器

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;

};
}

fragment

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>

最佳答案

改变

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

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

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

关于java - 在适配器和 fragment 中获取正确的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40633639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com