java - 将自定义 ListView 添加到 fragment

标签 java android

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

有什么我可以用它做的吗? 我应该尝试另一件事吗?

这是我想使用 ListView 的 Fragment

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);
  }
}

这是每个 ListView 项的可视化界面的 XML

<TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="" />

<TextView
    android:id="@+id/tv2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="" />

<TextView
    android:id="@+id/tv3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="" />

这是我创建的用于表示每个 ListView 项目的类

class Record{
  private String test, date;
  private int score;

  public Record(String test, int score, String date)
  {
    this.test= test;
    this.score= score;
    this.date = date;
  }

  public String getTest() {
    return test;
  }

  public int getScore(){
    return score;
  }

  public String getDate(){
    return date;
  }
}

如果我还有什么要展示的,请告诉我。 先谢谢你。

编辑:我修正了一些拼写错误。 fragment 中的这一行无法编译:

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

当我尝试运行它时显示此消息(我是 guest 是错误日志):

Error:(39, 40) error: constructor CustomAdapter in class RecordFragment.CustomAdapter cannot be applied to given types;

必需:AppCompatActivity 发现:FragmentActivity、int、ArrayList 原因:实际和形式参数列表的长度不同

最佳答案

根据你的问题,你正面临这个问题

after adding AppcompatActivity your app is not crashing but list is not showing anything

您的 Adapter 正在从您的 Activity 中请求 Context,它是 AppCompat Type 。因此,如果您的主机 Activity 未扩展 AppcompatActivity,它将崩溃

所以当你把它改成 AppcompatActivity 它不会崩溃

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

另一件事 CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList); 我不认为你的代码用这一行编译AdaptadorHistorial(AppCompatActivity 上下文){ super (上下文,R.layout.record_fragment,scoreList); 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();
}

关于java - 将自定义 ListView 添加到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46804121/

相关文章:

java - 如何在Java中正确比较整数

java - (Java) 在运行时更改 swing 组件的文本(资源包)

java - 如何在 ViewPager 中仅查看部分 Fragment

android - EventBus 不在新 Activity 上注册事件

java - 相对较大的 TreeMap 的更快序列化

java - 将 JScrollPane 内容打印为 pdf

android - 我该如何在后台使用此MP3?

android - 如何在android相机api上进行图像分析

java - MX Player 打开而不是通过 Intent 播放视频

android - 通过单击 ListView 中的任何项目来删除项目