gpt4 book ai didi

java - 错误 : no suitable constructor found for ArrayAdapter(Tab1Fragment, int,ArrayList <字符串>)

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:24 25 4
gpt4 key购买 nike

我是编程新手,一直在尝试将 ListView +数组适配器添加到学校项目的 fragment 中。已经提到过这个问题但有用。 https://stackoverflow.com/questions/32350275/no-suitable-constructor-found-for-arrayadaptermainlistactivity-getblogpoststask#=

package com.tabian.tabfragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import android.widget.Button;


public class Tab1Fragment extends Fragment {
private static final String TAG = "Tab1Fragment";

private ListView listOfLeagues;
final ArrayList<String> listItems = new ArrayList<String>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1_fragment,container,false);
listOfLeagues = (ListView) view.findViewById(R.id.listOfLeagues);

String[] values = new String[] { "World Cup", "Champions League" };

for(int i = 0; i < values.length; i++){
listItems.add(values[i]);
}

ArrayAdapter adapter = new ArrayAdapter<(Tab1Fragment.this,
android.R.layout.simple_list_item_1, listItems);
listOfLeagues.setAdapter(adapter);

listOfLeagues.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedLeague = listItems.get(position).toString();

Intent titleIntent = new Intent(view.getContext(), abc.class);

titleIntent.putExtra("league", selectedLeague);
startActivity(titleIntent);
}
});

return view;
}

}

最佳答案

ArrayAdapter 构造函数的第一个参数应该是 Context,但您传递的是 Fragment,即 Tab1Fragment.this

在生命周期中处于附加状态的 Fragment 中,您可以使用 getContext() 获取 Context。因此,请将 Tab1Fragment.this 替换为 getContext()

关于java - 错误 : no suitable constructor found for ArrayAdapter(Tab1Fragment, int,ArrayList <字符串>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51230284/

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