在RecyclerView(Android Studio)中显示Int ArrayList

时间:2017-07-24 12:26:02

标签: java android json arraylist android-recyclerview

我试图在RecyclerView中显示Integers的ArrayList,从JSON下载ArrayList中的Integer值,所有这些都完成了,我需要帮助的是在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,不知道这是否是正确的方法,但这是其他人似乎正在做的事情。

我知道它可能看起来很乱,但是因为我一直在google和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

3 个答案:

答案 0 :(得分:1)

从代码

中删除它
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

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

足以绑定Activity

中的视图
@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`

所以当你SetText() TextView时请使用Typecast Value

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

答案 1 :(得分:0)

试试这个

@Override
public void onBindViewHolder(MainAdapter.ViewHolder holder, int position)
{
    holder.rideHeight.setText(""+ rideList.get(position));
}

否则这个

 @Override
    public void onBindViewHolder(MainAdapter.ViewHolder holder, int position)
    {
        holder.rideHeight.setText(String.valueOf(rideList.get(position)));
    }

答案 2 :(得分:0)

确保在访问布局内的任何视图之前设置了内容视图。 将ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);移至super.onCreate(savedInstanceState);后面的行。

顺便说一下,没有必要使用findViewById()来访问回收站视图。在数据绑定中,有一种方便的方法,如

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