简体   繁体   English

将自定义列表视图添加到片段

[英]Add custom listview to a fragment

I am trying to work with a custom listview in a fragment, and I tried a lot of things, I watched videos and I looked for solutions in this site, but I get confused because it doesn't work.我正在尝试在片段中使用自定义列表视图,我尝试了很多东西,我观看了视频并在此站点中寻找解决方案,但我感到困惑,因为它不起作用。 My problem is that in the code, I can't make work the custom adapter, it crash when the fragment inflates, and with the "(AppCompatActivity)" added, it doesn't crash but the listview doesn't show anything.我的问题是,在代码中,我无法使用自定义适配器,它会在片段膨胀时崩溃,并且添加了“(AppCompatActivity)”,它不会崩溃,但列表视图没有显示任何内容。

Is there something I could do with this?有什么我可以做的吗? Should I try another thing?我应该尝试另一件事吗?

This is the Fragment I want to use the 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);
  }
}

This is the XML with the visual interface of each ListView item这是带有每个 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="" />

And this is the class I created to represent each ListView item这是我创建的用于表示每个 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;
  }
}

If there is something else I have to show tell me, please.如果还有什么我要展示的,请告诉我。 Beforehand, thank you.先谢谢了。

EDIT: I fixed some spelling mistakes.编辑:我修正了一些拼写错误。 And this line in the Fragment doesn't compile: Fragment 中的这一行无法编译:

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

This message is displayed when I try to run it (I guest is the error log):当我尝试运行它时会显示此消息(我来宾是错误日志):

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

required: AppCompatActivity found: FragmentActivity,int,ArrayList reason: actual and formal argument lists differ in length要求:AppCompatActivity 发现:FragmentActivity,int,ArrayList 原因:实际和形式参数列表的长度不同

According to your question you are facing this problem根据您的问题,您正面临这个问题

after adding AppcompatActivity your app is not crashing but list is not showing anything添加 AppcompatActivity 后,您的应用没有崩溃,但列表没有显示任何内容

Your Adapter is asking for Context from your Activity which is AppCompat Type .您的适配器正在从您的 Activity 请求 Context ,它是 AppCompat Type 。 So if when your host activity is not extending AppcompatActivity it will crash因此,如果当您的主机活动未扩展 AppcompatActivity 时,它将崩溃

So when you change it to AppcompatActivity it won't crash因此,当您将其更改为 AppcompatActivity 时,它不会崩溃

Now lets solve the problem of showing a blank listView you didn't override the parent method getCount() .现在让我们解决显示一个空白 listView 的问题,你没有覆盖父方法getCount() In the getCount() method return size items in your List.getCount()方法中返回列表中的大小项目。

Another thing CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList);另一件事CustomAdapter adapter = new CustomAdapter(RecordFragment.this.getActivity(), android.R.layout.simple_list_item_1, scoreList); I don't think your code compiles with this line as your AdaptadorHistorial(AppCompatActivity context){ super(context, R.layout.record_fragment, scoreList); appCompatActivity = context; }我不认为你的代码用这一行编译为你的AdaptadorHistorial(AppCompatActivity context){ super(context, R.layout.record_fragment, scoreList); appCompatActivity = context; } AdaptadorHistorial(AppCompatActivity context){ super(context, R.layout.record_fragment, scoreList); appCompatActivity = context; } AdaptadorHistorial(AppCompatActivity context){ super(context, R.layout.record_fragment, scoreList); appCompatActivity = context; } custom adapter is taking one parameter but you are passing two AdaptadorHistorial(AppCompatActivity context){ super(context, R.layout.record_fragment, scoreList); appCompatActivity = context; }自定义适配器正在一个参数,但你传递两个

your constructor should be like this你的构造函数应该是这样的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM