gpt4 book ai didi

java - Arraylist 显示此值 PlanetData@adf4bee 而不是附加值

转载 作者:行者123 更新时间:2023-12-02 10:36:38 26 4
gpt4 key购买 nike

我不太确定我的主题是否提出了正确的问题,但情况是这样的。我正在开发一个在不同类中执行多个计算的应用程序。计算正确完成,我可以看到它们在 logcat 中。现在我想在 recyclerview 中显示它们,为此我尝试在执行 recyclerview 之前将它们添加到数组列表中。我的数组列表正在获取标题中显示的值,但它没有显示在我的回收 View 中。这是我的代码,但根据我查找的研究,我看不出我做错了什么。

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

fragment Activity

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

In my logcat I'm getting the following response in my arraylist: 11-10 22:02:52.581 13826-13826/com.ksburneytwo.planetmathtest I/System.out: This is the 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.ksburneytwo.planetmathtest.PlanetData@2a5d908, com.ksburneytwo.planetmathtest.PlanetData@cee08a1, com.ksburneytwo.planetmathtest.PlanetData@580eac6, com.ksburneytwo.planetmathtest.PlanetData@a907a87]

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

最佳答案

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

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

您可以使用 IDE 生成 toString() 方法,并选择要在输出中包含的字段。

关于java - Arraylist 显示此值 PlanetData@adf4bee 而不是附加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53245681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com