toast message implemented
This commit is contained in:
parent
6537d97d8a
commit
8118910a12
|
@ -36,4 +36,13 @@ public class Location {
|
|||
public void setCounty(String county) {
|
||||
this.county = county;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Location{" +
|
||||
"location='" + location + '\'' +
|
||||
", city='" + city + '\'' +
|
||||
", county='" + county + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import android.support.v7.widget.LinearLayoutManager;
|
|||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -17,7 +18,7 @@ import at.fhj.airkoality.R;
|
|||
import at.fhj.airkoality.model.Location;
|
||||
import at.fhj.airkoality.ui.adapter.LocationListAdapter;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
public class MainActivity extends AppCompatActivity implements LocationListAdapter.ItemClickListener {
|
||||
private RecyclerView locationList;
|
||||
|
||||
@Override
|
||||
|
@ -38,11 +39,16 @@ private RecyclerView locationList;
|
|||
locations.add(new Location("Daheim7", "Graz" , "Österreich"));
|
||||
locations.add(new Location("Daheim8", "Graz" , "Österreich"));
|
||||
|
||||
LocationListAdapter adapter = new LocationListAdapter(locations);
|
||||
LocationListAdapter adapter = new LocationListAdapter(locations, this);
|
||||
|
||||
locationList.setAdapter(adapter);
|
||||
locationList.setLayoutManager(new LinearLayoutManager(this));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClicked(Location location, int position) {
|
||||
Toast.makeText(this, "Clicked " + location, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,9 +18,11 @@ public class LocationListAdapter extends RecyclerView.Adapter<LocationListAdapte
|
|||
|
||||
|
||||
private List<Location> locations;
|
||||
private ItemClickListener listener;
|
||||
|
||||
public LocationListAdapter(List<Location> locations){
|
||||
public LocationListAdapter(List<Location> locations, ItemClickListener listener){
|
||||
this.locations = locations;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
|
||||
|
@ -37,6 +39,14 @@ public class LocationListAdapter extends RecyclerView.Adapter<LocationListAdapte
|
|||
|
||||
Context context = locationItemViewHolder.tvLocation.getContext();
|
||||
|
||||
locationItemViewHolder.cvLocationItem.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Location clickedLocation = locations.get(locationItemViewHolder.getAdapterPosition());
|
||||
listener.onItemClicked(clickedLocation, locationItemViewHolder.getAdapterPosition());
|
||||
}
|
||||
});
|
||||
|
||||
locationItemViewHolder.tvLocation.setText(context.getString(R.string.location_label) + " " + locations.get(position).getLocation());
|
||||
locationItemViewHolder.tvCity.setText(context.getString(R.string.city_label) + " " + locations.get(position).getLocation());
|
||||
locationItemViewHolder.tvCountry.setText(context.getString(R.string.country_label) + " " + locations.get(position).getLocation());
|
||||
|
@ -66,4 +76,9 @@ public class LocationListAdapter extends RecyclerView.Adapter<LocationListAdapte
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
public interface ItemClickListener {
|
||||
void onItemClicked(Location location, int position);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,8 +34,8 @@
|
|||
## 08.03.2019 - Lab 3
|
||||
|
||||
Context -> weiß was in der App gerade "abgeht" (Java Objekt)
|
||||
- Activity oder Application
|
||||
- Immer Context der aktuellen Activity verwenden
|
||||
- Activity oder Application
|
||||
- Immer Context der aktuellen Activity verwenden
|
||||
|
||||
Best Practise kennengelernt um große Listen darzustellen
|
||||
|
||||
|
|
Loading…
Reference in New Issue