FI am trying to display a custom ListView using a custom adapter for a Car class. However, when I run the app in the emulator, the activity screen is black. I added some static text to the XML layout to debug, and the text appeared, so the issue seems to be with the ListView or setAdapter().
Here’s my setup:
XML Layout (activity_main.xml):
`
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"><ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/cars_list"/></LinearLayout>
`
This is my main activity:
package com.example.carscustomadapter;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.ListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView carsList = findViewById(R.id.cars_list); List<Car> cars = new ArrayList<>(); cars.add(new Car(1000,20,2)); cars.add(new Car(1000,40,2)); cars.add(new Car(1000,60,8)); cars.add(new Car(1000,100,5)); cars.add(new Car(1000,20,2)); cars.add(new Car(1000,40,2)); cars.add(new Car(1000,60,8)); cars.add(new Car(1000,100,5)); cars.add(new Car(1000,20,2)); cars.add(new Car(1000,40,2)); cars.add(new Car(1000,60,8)); cars.add(new Car(1000,100,5)); cars.add(new Car(1000,20,2)); cars.add(new Car(1000,40,2)); cars.add(new Car(1000,60,8)); cars.add(new Car(1000,100,5)); CarsAdapter carsAdapter = new CarsAdapter(cars); carsList.setAdapter(carsAdapter); }}
Custom Adapter (CarsAdapter)
public class CarsAdapter extends BaseAdapter implements View.OnClickListener { private List<Car> cars; public CarsAdapter(List<Car> cars) { this.cars = cars; } @Override public int getCount() { return 0; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (view == null) view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.car_layout, viewGroup, false); TextView kmTv = view.findViewById(R.id.km_tv); TextView fuelTv = view.findViewById(R.id.fuel_tv); Button driveBtn = view.findViewById(R.id.drive_btn); driveBtn.setTag(i); Car car = cars.get(i); kmTv.setText(car.getMileage() +""); fuelTv.setText(car.getFuel() +""); driveBtn.setOnClickListener(this); return view; } @Override public void onClick(View v) { LinearLayout root = (LinearLayout) v.getParent(); EditText editText = root.findViewById(R.id.km_et); Car car = cars.get((Integer) v.getTag()); car.drive(Integer.parseInt(editText.getText().toString())); notifyDataSetChanged(); }}
The Issue::When running the app, the screen is black, and the ListView does not display anything. I suspect the issue might be with:
1.The adapter configuration or implementation.2.Incorrect data being provided to the ListView.3.Layout issues with the car_layout.xml (not provided here but could be relevant).
What I Tried:1.Adding static text to the activity_main.xml to confirm the layout is 2.rendering properly.3.Verifying that setAdapter() is called.4.Debugging the app, but I’m not sure where the problem lies.
What I ExpectI expect to see a ListView populated with rows of Car objects. Each row should display the mileage and fuel, with a button for actions.