gpt4 book ai didi

java - 在 RecyclerView 中显示 Int ArrayList (Android Studio)

转载 作者:行者123 更新时间:2023-11-30 00:26:27 27 4
gpt4 key购买 nike

我正在尝试在 RecyclerView 中显示整数的 ArrayList,ArrayList 中的整数值是从 JSON 下载的,所有这些都已完成,我需要帮助的是将它们显示在 RecyclerView 中。

这是 MainActivity 的一部分:

    public class MainActivity extends AppCompatActivity {

private ActivityMainViewModel viewmodel;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new MainAdapter(JsonData.rideList);
recyclerView.setAdapter(mAdapter);

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

viewmodel = new ActivityMainViewModel();
binding.setViewmodel(viewmodel);
}

这是 MainAdapter 类:

    class MainAdapter extends android.support.v7.widget.RecyclerView.Adapter<MainAdapter.ViewHolder> {

int ride;
private ArrayList<Integer> rideList;

public MainAdapter(ArrayList<Integer> rideList)
{
this.rideList = rideList;
}

@Override
public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(MainAdapter.ViewHolder holder, int position)
{
holder.rideHeight.setText(ride);
}

@Override
public int getItemCount()
{
return (null != rideList ? rideList.size() : 0);
}

public class ViewHolder extends RecyclerView.ViewHolder {

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

rideHeight = (TextView) itemView.findViewById(R.id.rideHeight_textView);
}
}

顺便说一句,rideHeight_textView 是 RecyclerView 内部的一个 TextView,不知道这是否是正确的做法,但其他人似乎都在这样做。

我知道它可能看起来一团糟,但那是因为我一直在谷歌和 youtube 上搜索如何使用 RecyclerView,所以我可能混合了不同的教程并自己尝试了一些东西。我阅读或观看的所有教程都在使用对象的 ArrayList,而我使用的是 Int。

谢谢!

编辑:显然我遇到了这个致命异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.remi.uppgift/com.example.remi.uppgift.MainActivity}: android.view.InflateException: Binary XML file line #91: RecyclerView has no LayoutManager

最佳答案

只需从您的代码中删除它

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

viewmodel = new ActivityMainViewModel();
binding.setViewmodel(viewmodel);

Activity中绑定(bind)view就够了

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new MainAdapter(JsonData.rideList);
recyclerView.setAdapter(mAdapter);
}

还有

rideList.get(position) this is an `Interer` Value not `String`

所以当你在 TextViewSetText() 时请输入值

holder.rideHeight.setText(String.valueOf(rideList.get(position)));

关于java - 在 RecyclerView 中显示 Int ArrayList (Android Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45280622/

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