Skip to content

Commit

Permalink
Now instead of a toast a text is displayed when lists are empty:
Browse files Browse the repository at this point in the history
  • Loading branch information
carlostojal committed Jul 10, 2019
1 parent 755a9ce commit f4f9102
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 79 deletions.
Binary file modified apk/MyVehicles.apk
Binary file not shown.
70 changes: 42 additions & 28 deletions app/src/main/java/com/carlostojal/myvehicles/Cars.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

public class Cars extends Fragment {

Expand All @@ -32,36 +34,48 @@ public class Cars extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cars, container, false);

View view;
vehicleManager = new VehicleManager();
cars = vehicleManager.loadVehicles(getContext(),1);
if(cars.size()==0)
Toast.makeText(getContext(), "No cars were found.", Toast.LENGTH_SHORT).show();

carList = view.findViewById(R.id.car_list);
ArrayAdapter carAdapter = new CarAdapter(getContext(),cars);
carList.setAdapter(carAdapter);

addCar = (Button) view.findViewById(R.id.add_vehicle);
addCar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddCar();
}
});

carList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Vehicle selectedVehicle = (Vehicle) adapterView.getItemAtPosition(i);
Intent intent = new Intent(Cars.this.getActivity(), VehicleDetails.class);
intent.putExtra("registration",selectedVehicle.getRegistration());
intent.putExtra("type",1);
startActivity(intent);
}
});

if(cars.size()==0) {
view = inflater.inflate(R.layout.empty_list, container, false);
TextView label = (TextView) view.findViewById(R.id.textView);
label.setText("No cars were found.");
addCar = (Button) view.findViewById(R.id.button);
addCar.setText("Add car");
addCar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddCar();
}
});
}
else {
view = inflater.inflate(R.layout.fragment_cars, container, false);

carList = view.findViewById(R.id.car_list);
ArrayAdapter carAdapter = new CarAdapter(getContext(), cars);
carList.setAdapter(carAdapter);

addCar = (Button) view.findViewById(R.id.add_vehicle);
addCar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddCar();
}
});

carList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Vehicle selectedVehicle = (Vehicle) adapterView.getItemAtPosition(i);
Intent intent = new Intent(Cars.this.getActivity(), VehicleDetails.class);
intent.putExtra("registration", selectedVehicle.getRegistration());
intent.putExtra("type", 1);
startActivity(intent);
}
});
}
return view;
}

Expand Down
63 changes: 38 additions & 25 deletions app/src/main/java/com/carlostojal/myvehicles/Motorcycles.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
Expand All @@ -32,35 +33,47 @@ public class Motorcycles extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_motorcycles, container, false);

View view;
motorcycles = vehicleManager.loadVehicles(getContext(),2);
if(motorcycles.size()==0)
Toast.makeText(getContext(), "No motorcycles were found.", Toast.LENGTH_SHORT).show();

motorcycleList = view.findViewById(R.id.motorcycles_list);
ArrayAdapter motorcycleAdapter = new MotorcycleAdapter(getContext(),motorcycles);
motorcycleList.setAdapter(motorcycleAdapter);
if(motorcycles.size()==0) {
view = inflater.inflate(R.layout.empty_list, container, false);

addMotorcycle = (Button) view.findViewById(R.id.add_motorcycle);
addMotorcycle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddMotorcycle();
}
});
TextView label = (TextView) view.findViewById(R.id.textView);
label.setText("No motorcycles were found.");
addMotorcycle = (Button) view.findViewById(R.id.button);
addMotorcycle.setText("Add motorcycle");
addMotorcycle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddMotorcycle();
}
});
}
else {
view = inflater.inflate(R.layout.fragment_motorcycles, container, false);
motorcycleList = view.findViewById(R.id.motorcycles_list);
ArrayAdapter motorcycleAdapter = new MotorcycleAdapter(getContext(), motorcycles);
motorcycleList.setAdapter(motorcycleAdapter);

motorcycleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Vehicle selectedVehicle = (Vehicle) adapterView.getItemAtPosition(i);
Intent intent = new Intent(Motorcycles.this.getActivity(), VehicleDetails.class);
intent.putExtra("registration",selectedVehicle.getRegistration());
intent.putExtra("type",2);
startActivity(intent);
}
});
addMotorcycle = (Button) view.findViewById(R.id.add_motorcycle);
addMotorcycle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddMotorcycle();
}
});

motorcycleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Vehicle selectedVehicle = (Vehicle) adapterView.getItemAtPosition(i);
Intent intent = new Intent(Motorcycles.this.getActivity(), VehicleDetails.class);
intent.putExtra("registration", selectedVehicle.getRegistration());
intent.putExtra("type", 2);
startActivity(intent);
}
});
}
return view;
}

Expand Down
64 changes: 38 additions & 26 deletions app/src/main/java/com/carlostojal/myvehicles/Other.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
Expand All @@ -34,35 +35,46 @@ public class Other extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_other, container, false);

View view;
others = vehicleManager.loadVehicles(getContext(),3);
if(others.size()==0)
Toast.makeText(getContext(), "No vehicles were found.", Toast.LENGTH_SHORT).show();

otherList = view.findViewById(R.id.other_list);
ArrayAdapter otherAdapter = new OtherAdapter(getContext(),others);
otherList.setAdapter(otherAdapter);

addOther = (Button) view.findViewById(R.id.add_other);
addOther.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddOther();
}
});
if(others.size()==0) {
view = inflater.inflate(R.layout.empty_list, container, false);
TextView label = (TextView) view.findViewById(R.id.textView);
label.setText("No vehicles were found.");
addOther = (Button) view.findViewById(R.id.button);
addOther.setText("Add vehicle.");
addOther.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddOther();
}
});
}
else {
view = inflater.inflate(R.layout.fragment_other, container, false);
otherList = view.findViewById(R.id.other_list);
ArrayAdapter otherAdapter = new OtherAdapter(getContext(), others);
otherList.setAdapter(otherAdapter);

otherList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Vehicle selectedVehicle = (Vehicle) adapterView.getItemAtPosition(i);
Intent intent = new Intent(Other.this.getActivity(), VehicleDetails.class);
intent.putExtra("registration",selectedVehicle.getRegistration());
intent.putExtra("type",3);
startActivity(intent);
}
});
addOther = (Button) view.findViewById(R.id.add_other);
addOther.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onAddOther();
}
});

otherList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Vehicle selectedVehicle = (Vehicle) adapterView.getItemAtPosition(i);
Intent intent = new Intent(Other.this.getActivity(), VehicleDetails.class);
intent.putExtra("registration", selectedVehicle.getRegistration());
intent.putExtra("type", 3);
startActivity(intent);
}
});
}
return view;
}

Expand Down
37 changes: 37 additions & 0 deletions app/src/main/res/layout/empty_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
android:background="@color/colorPrimary"
android:textColor="@color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<resources>
<string name="app_name">MyVehicles</string>
<string name="no_cars">No cars were found.</string>
<string name="no_motorcycles">No motorcycles were found.</string>
<string name="no_vehicles">No vehicles were found.</string>
<string name="add_car">Add Car</string>
<string name="add_motorcycle">Add Motorcycle</string>
<string name="add_vehicle">Add Vehicle</string>
Expand Down

0 comments on commit f4f9102

Please sign in to comment.