簡體   English   中英

ArrayList 未顯示在 RecyclerView 中,但未顯示錯誤

[英]ArrayList not displaying in RecyclerView but no errors shown

我有一個未在 RecyclerView 中顯示的數組列表。 arraylist 有數據,但我的 RecyclerView Adapter 沒有顯示錯誤,我的片段活動也沒有顯示任何錯誤。 我對編程錯誤所在的地方完全不知所措。 getItemCount 似乎是正確的,持有者似乎是正確的,而 Fragment 似乎是正確的,但我知道某處有錯誤。 這是我的代碼:

分段:

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;


}

}

這是 PlanetData 類:

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

}

這是我的 RecyclerView 適配器:

public class PlanetRecyclerViewAdapter extends RecyclerView.Adapter<PlanetRecyclerViewAdapter.ViewHolder> {

private List<PlanetData> mPlanetDataList;
Context mContext;

public static class ViewHolder extends RecyclerView.ViewHolder{

    public TextView currentRA;

    public ViewHolder(View itemView) {
        super(itemView);

        currentRA = (TextView) itemView.findViewById(R.id.planet_location);


    }

}

public PlanetRecyclerViewAdapter(List<PlanetData> mPlanetDataList, Context mContext){
    this.mPlanetDataList = mPlanetDataList;
    this.mContext = mContext;
}

@Override
public PlanetRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.planet_recycler_item,parent, false);



    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder( PlanetRecyclerViewAdapter.ViewHolder holder, int position) {
holder.currentRA.setText(mPlanetDataList.get(position).getPlanetRA());

}

@Override
public int getItemCount() {
    return mPlanetDataList.size();
}

}

您尚未填充項目。

items = planetList();

我沒有看到您實際上在items列表中添加了任何內容。

您在onCreateView()調用了planetList() ,但您沒有使用它的結果,並且planetList()不會以任何方式影響items :它創建自己的ArrayList 並返回它。

planetList()方法中刪除planetValues並直接引用items

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

或者在調用時將planetList()的結果設置為items

items.addAll(planetList());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM