ArrayList未显示在RecyclerView中,但未显示任何错误

时间:2018-11-12 17:42:13

标签: java android android-recyclerview

我有一个不在RecyclerView中显示的数组列表。 arraylist有数据,但是我的RecyclerView适配器没有显示错误,我的片段活动也没有显示错误。我完全迷失了编程错误所在。该getItemCount似乎是正确的,持有人似乎是正确的,而Fragment似乎是正确的,但是我知道某个地方有一个错误。这是我的代码:

片段:

/seq/pacbio/r54097_20170511_114349/1_A01:
  m54097_170511_115331.adapters.fasta
  m54097_170511_115331.scraps.bam
  m54097_170511_115331.scraps.bam.pbi
  m54097_170511_115331.sts.xml
  m54097_170511_115331.subreads.bam
  m54097_170511_115331.subreads.bam.pbi
  m54097_170511_115331.subreadset.xml

}

这是PlanetData类:

public class TestFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
List<PlanetData> items = new ArrayList<>();
RecyclerView mRecyclerView;
PlanetRecyclerViewAdapter adapter;

private OnFragmentInteractionListener mListener;

public TestFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);


    }


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


    planetList();



        // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_test, container, false);



    mRecyclerView = (RecyclerView)view.findViewById(R.id.planet_recycler_view);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(),DividerItemDecoration.VERTICAL));

    adapter = new PlanetRecyclerViewAdapter(items, mRecyclerView.getContext());
    mRecyclerView.setAdapter(adapter);




        return view;
    }


// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

private List<PlanetData> planetList() {
    List<PlanetData> planetvalues = new ArrayList<>();

    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData("12"));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Mercury.getMercuryRA())));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Venus.getVenusRA())));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Moon.getMoonRA())));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Mars.getMarsRA())));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Jupiter.getJupiterRA())));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Saturn.getSaturnRA())));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Uranus.getUranusRA())));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Neptune.getNeptuneRA())));
    planetvalues.add(new com.ksburneytwo.planetmathtest.PlanetData(Double.toString(Pluto.getPlutoRA())));

    System.out.println("This is Arraylist:" + planetvalues);

    return planetvalues;


}

这是我的RecyclerView适配器:

 public class PlanetData {
private String PlanetRA;

public PlanetData(String PlanetRA) {
    this.PlanetRA = PlanetRA;
}

@Override
public String toString() {
    return PlanetRA;
}

public String getPlanetRA (){
    return PlanetRA;
}

public void setPlanetRA(String PlanetRA){
    this.PlanetRA = PlanetRA;
}

}

}

2 个答案:

答案 0 :(得分:0)

您没有填充项目。

items = planetList();

答案 1 :(得分:0)

我看不到您实际上在items列表中添加了任何内容。

您在planetList()中调用onCreateView(),但是您没有使用它的结果,并且planetList()不会以任何方式影响items:拥有ArrayList并返回它。

planetValues方法中删除planetList()并直接引用items

private void planetList() { //changed signature to "void"
    items.add(...);
    items.add(...);
    //etc
}

或在调用时将planetList()的结果设置为items

items.addAll(planetList());