Java 将自定义listview添加到片段

Java 将自定义listview添加到片段,java,android,Java,Android,我试图在一个片段中使用自定义listview,我尝试了很多东西,我观看了视频,在这个网站上寻找解决方案,但我感到困惑,因为它不起作用。 我的问题是,在代码中,我无法使自定义适配器工作,当片段膨胀时,它会崩溃,添加“(AppCompatActivity)”后,它不会崩溃,但listview不会显示任何内容 我能帮上什么忙吗? 我应该试试别的吗 这是我想使用ListView的片段 public class RecordFragment extends Fragment { private Arr

我试图在一个片段中使用自定义listview,我尝试了很多东西,我观看了视频,在这个网站上寻找解决方案,但我感到困惑,因为它不起作用。 我的问题是,在代码中,我无法使自定义适配器工作,当片段膨胀时,它会崩溃,添加“(AppCompatActivity)”后,它不会崩溃,但listview不会显示任何内容

我能帮上什么忙吗? 我应该试试别的吗

这是我想使用ListView的片段

public class RecordFragment extends Fragment {

 private ArrayList<Record> scoreList;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_record, container, false);
    scoreList= new ArrayList<>();

    scoreList.add(new Record("Math", 10, "9/1/2017 13:45"));
    scoreList.add(new Record("Math", 8, "7/5/2017 10:50"));
    scoreList.add(new Record("Marh", 4, "7/7/2017 16:30"));

    CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList);

    ListView list1 = (ListView)view.findViewById(R.id.list1);
    list1.setAdapter(adapter);

    return view;
 }

 private class CustomAdapter extends ArrayAdapter<Record>{
    AppCompatActivity appCompatActivity;

    CustomAdapter(AppCompatActivity context){
        super(context, R.layout.record_fragment, scoreList);
        appCompatActivity = context;
     }

     public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = appCompatActivity.getLayoutInflater();
        View item = inflater.inflate(R.layout.record_fragment, null);

        TextView tv1, tv2, tv3;
        tv1 = (TextView)item.findViewById(R.id.tv1);
        tv2 = (TextView)item.findViewById(R.id.tv2);
        tv3 = (TextView)item.findViewById(R.id.tv3);

        tv1.setText(scoreList.get(position).getTest());
        tv2.setText(scoreList.get(position).getScore());
        tv3.setText(scoreList.get(position).getDate());

        return item;
    }
 }

  public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
  }
}
如果还有什么我要展示的,请告诉我。 事先,谢谢

编辑:我修正了一些拼写错误。片段中的这一行没有编译:

CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList);
当我尝试运行此消息时会显示此消息(I guest是错误日志):

必需:AppCompative活动 找到:FragmentActivity,int,ArrayList
原因:实际参数列表和正式参数列表的长度不同,这取决于您所面临的问题

添加AppCompativeActivity后,您的应用程序没有崩溃,但列表中没有显示任何内容

您的适配器正在请求来自您的活动的上下文,它是AppCompat类型。因此,如果主机活动不扩展AppcompatActivity,它将崩溃

因此,当您将其更改为AppcompatActivity时,它不会崩溃

现在让我们来解决显示空白listView的问题 您没有重写父方法
getCount()
。在
getCount()
方法中,返回列表中的大小项目


另一件事
CustomAdapter=newcustomadapter(RecordFragment.this.getActivity(),android.R.layout.simple\u list\u item\u 1,scoreList)我不认为您的代码是以这一行作为
AdapterHistorial(AppCompativeContext)编译的{
super(上下文、R.layout.record_片段、记分表);
appCompatActivity=上下文;
}
自定义适配器接受一个参数,但您要传递两个参数

您的构造函数应该是这样的

AdaptadorHistorial(Context context , List<Record> scoreList){
    super(context, R.layout.record_fragment, scoreList);
    this.context = context;
    this.items = scoreList;

 }



@override
public int getCount(){
 return items.size();
}
adaptidatorhistorial(上下文上下文、列表分数列表){
super(上下文、R.layout.record_片段、记分表);
this.context=上下文;
this.items=得分表;
}
@凌驾
public int getCount(){
返回items.size();
}

请也添加错误日志。您确定您的类
记录已编译吗?构造函数看起来很奇怪。你能不能也添加fragment_record.xml文件?因为这可能是布局问题。我不认为你的代码用这段代码编译;您的适配器构造函数接受一个参数,您正在传递三个HOW!!!并且您的适配器正在接受AppCompat活动,因此如果您使用RecordFragment.this.getActivity(),并且如果父活动不是AppCompat,则无论如何都会崩溃。只需使用上下文,您不需要传递活动的实例对不起,我不是要写两个参数,它是相同的,我已经更正了
CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList);
Error:(39, 40) error: constructor CustomAdapter in class RecordFragment.CustomAdapter cannot be applied to given types;
AdaptadorHistorial(Context context , List<Record> scoreList){
    super(context, R.layout.record_fragment, scoreList);
    this.context = context;
    this.items = scoreList;

 }



@override
public int getCount(){
 return items.size();
}