For some reason I don't see the list view on my emulator, can somebody tell me why? I have a custom adapter for a car class I built.
Here is my code: 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);
}
}
This is my adapter:
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();
}
}
I tried to display a custom list view, however, I am getting a black activity, I tried To debug it and added some text to the layout to see if the text get added, and indeed it was added. So something is not working with the setAdapter()?
thanks in advance screen Image
CodePudding user response:
In you CarsAdapter
you are returning 0
from the getCount()
method. The adapter uses this to know how many items to draw in the ListView. It should return the count of the items in the cars list