This question already has answers here:
How do I print my Java object without getting “SomeType@2f92e0f4”?
                            
                                (9个答案)
                            
                    
                2年前关闭。
        

    

我不确定我的主题是否提出了正确的问题,但事实是这样。我正在一个应用程序上执行单独的类中的多个计算。计算正确完成,我能够看到它们在logcat中。现在,我想在recyclerview中显示它们,并为此尝试在执行recyclerview之前将它们添加到arraylist中。我的数组列表正在标题中显示值,而在recyclerview中没有显示值。这是我的代码,但是基于我的研究,我看不到自己在做什么错。

ArrayList代码:

public class PlanetData {
private String planetRA;

private PlanetData(String planetRA){
    this.planetRA = planetRA;
}

public String getPlanetRA(){
    return planetRA;
}

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

public static void main(String [] args){
        List<PlanetData> planetvalues = new ArrayList<>();

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

            System.out.println("This is the arraylist" + planetvalues);

}




        }


RecyclerView适配器

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

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


片段活动

public class TestFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private TextView planetLongitude;
private TextView planetLatitude;
private TextView planetTime;
private TextView SunRA;
private TextView SunDEC;
RecyclerView mRecyclerView;
PlanetRecyclerViewAdapter adapter;
List<PlanetData> items = new ArrayList<>();

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

Mercury test;

private OnFragmentInteractionListener mListener;

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

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment TestFragment.
 */
// TODO: Rename and change types and number of parameters
public static TestFragment newInstance(String param1, String param2) {
    TestFragment fragment = new TestFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

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



    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);



    }

}

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

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



  在我的logcat中,我在arraylist中得到以下响应:
  11-10 22:02:52.581 13826-13826 / com.ksburneytwo.planetmathtest I / System.out:这是arraylist [com.ksburneytwo.planetmathtest.PlanetData@adf4bee,com.ksburneytwo.planetmathtest.PlanetData@24a088f,com。 ksburneytwo.planetmathtest.PlanetData@5bb671c,com.ksburneytwo.planetmathtest.PlanetData@5dd9f25,com.ksburneytwo.planetmathtest.PlanetData@17838fa,com.ksburneytwo.planetmathtest.PlanetData@ce89eab,com.ksburneytwo5.com ksburneytwo.planetmathtest.PlanetData @ cee08a1,com.ksburneytwo.planetmathtest.PlanetData @ 580eac6,com.ksburneytwo.planetmathtest.PlanetData @ a907a87]


这些值应该是我计算的结果。

最佳答案

默认情况下,toString()方法将导致packagename.ClassName@hashcode

在您的PlanetData类中,您需要重写toString()方法,以便它可以返回适当的字段及其值。

您可以使用IDE生成toString()方法,然后选择要在输出中包括哪些字段。

09-11 22:48