所以我正在使用我的 android 应用程序从 firebase 获取数据,基本上是我保存到特定类的 ArrayList 中的汽车列表。我想显示一个 CardView
列表,每个列表都有一个汽车对象,换句话说,每张卡片都显示有关该汽车的信息(型号、公里数、价格等)。我知道我应该这样做的方法是在我的布局中使用 ListView,但我无法让适配器工作。
想法是它显示类似这样的内容,但带有汽车照片和其他内容: enter image description here
自从我最近开始研究这种类型的结构以来,我不是很熟悉它,但我想我了解它是如何工作的。我制作了 cardview 布局模型来显示每辆车,并尝试制作 ViewHolder
类。谢谢指教!我的代码:
车辆
类:
public class Vehicle {
int anio, cv, kms, puertas;
double precio;
String combustible, foto, marca, modelo, palanca;
public Vehicle() {
} //setters and getters are too, but I omitted them
我的cardview_template.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:padding="16dp"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/car_photo"
android:src="@drawable/ic_home_icon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/marca"
android:text="Mercedes-Benz"
android:layout_toRightOf="@+id/car_photo"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/kms"
android:text="128000"
android:layout_toRightOf="@+id/car_photo"
android:layout_below="@+id/marca"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/precio"
android:text="2018"
android:layout_toRightOf="@+id/car_photo"
android:layout_below="@+id/kms"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
mi MainActivity
中方法onCreate()
的部分代码:
我编辑了代码。请参阅下面的编辑
最后,我用于适配器
的类。我认为问题出在这里:
我编辑了类(class)。请参阅下面的编辑
编辑 所以按照建议,我用 RecyclerView 更改了 ListView,但它也没有用。我没有收到任何错误:CardViews 根本没有出现在我的 RecyclerView 中。
这是我的新适配器类和 MainActivity
onCreate()
中的新代码
适配器:
public class VehicleListAdapter extends RecyclerView.Adapter<VehicleListAdapter.VehicleViewHolder> {
private ArrayList<Vehicle> mVehicleList;
public static class VehicleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public TextView mTextView3;
public VehicleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.car_photo);
mTextView1 = itemView.findViewById(R.id.marca);
mTextView2 = itemView.findViewById(R.id.modelo);
mTextView3 = itemView.findViewById(R.id.precio);
}
}
public VehicleListAdapter(ArrayList<Vehicle> vehicleslist) {
mVehicleList = vehicleslist;
}
@NonNull
@Override
public VehicleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//Links the cardview layout and prepares the ViewHolder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_template, parent, false);
VehicleViewHolder vvh = new VehicleViewHolder(v);
return vvh;
}
@Override
public void onBindViewHolder(@NonNull VehicleViewHolder holder, int position) {
/*
* Links the layout elements with the variables in this class
* and put the object values in them
*/
Vehicle currentVehicle = mVehicleList.get(position);
//Uses Picasso librarie to set the image in the holder's ImageView
Picasso.get().load(currentVehicle.foto).into(holder.mImageView);
holder.mTextView1.setText(currentVehicle.getMarca());
holder.mTextView2.setText(currentVehicle.getModelo());
holder.mTextView3.setText(String.valueOf(currentVehicle.getPrecio()));
}
@Override
public int getItemCount() {
return mVehicleList.size();
}
主要 Activity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
(...)
//Prepares the recycler view and shows the data
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new VehicleListAdapter(vehiclesList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
EDIT2
好吧,我遇到了一件非常奇怪的事情。如果我在 onCreate()
中的任何地方放置一个调试点,然后执行应用程序调试,它就可以正常工作。我完全迷路了。
如果我不这样做,它会显示如下:
它在模拟器和我的实际手机中都有效。
最佳答案
首先你应该使用 recyclerView 而不是 ListView。
这是你错过的:
在 onCreate 方法中替换为:
VehicleListAdapter adapter = new VehicleListAdapter(this, R.layout.content_main, vehiclesList);
与
VehicleListAdapter adapter = new VehicleListAdapter(this, R.layout.cardview_template, vehiclesList);
关于java - 如何在 ListView 中从 ArrayList 设置 CardViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56309538/