Skip to content

Instantly share code, notes, and snippets.

@wemakeweb
Created November 19, 2014 18:46
Show Gist options
  • Save wemakeweb/fe35f2e87ad5e034472f to your computer and use it in GitHub Desktop.
Save wemakeweb/fe35f2e87ad5e034472f to your computer and use it in GitHub Desktop.
package com.example.app;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
private ListView list;
private Location locations;
private Spinner spinner;
private ArrayList<Location> totalLocations=new ArrayList<Location>();
private ArrayList<Location> sortedLocations= new ArrayList<Location>();
private String[] ratings={"1 Stern","2 Sterne","3 Sterne","4 Sterne","5 Sterne"};
private ArrayAdapter<Location> adapter;
ArrayAdapter<String> spinnerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null){
list= (ListView) findViewById(R.id.listViewListNightlife);
spinner= (Spinner) findViewById(R.id.spinnerListRatings);
setKneipenListe();
}
this.sortedLocations = (ArrayList<Location>) this.totalLocations.clone();
this.adapter= new MyAdapter(this, this.sortedLocations);
list.setAdapter(adapter);
this.spinnerAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ratings);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
}
public class MyAdapter extends ArrayAdapter<Location>{
public MyAdapter(Context c, ArrayList<Location> list){
super(c,R.layout.list_row,list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View root= convertView;
TextView name= null;
TextView adresse=null;
TextView type= null;
TextView rating=null;
if(root==null){
root= getLayoutInflater().inflate(R.layout.list_row, parent, false);
}
name= (TextView) root.findViewById(R.id.textViewListRowHeader);
adresse= (TextView) root.findViewById(R.id.textViewListRowAddresse);
type=(TextView) root.findViewById(R.id.textViewListRowType);
rating= (TextView) root.findViewById(R.id.textViewListRowRatingStars);
name.setText(""+getItem(position).getName()+"");
adresse.setText(""+getItem(position).getAdresse()+"");
type.setText(""+ getItem(position).getType()+"");
rating.setText(""+ getItem(position).getRating()+"");
return root;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
int ratingSpinner=position+1;
int ratingList;
this.adapter.clear();
for(int i=0; i< totalLocations.size();i++){
ratingList= totalLocations.get(i).getRating();
if(ratingList>=ratingSpinner){
this.adapter.add(totalLocations.get(i));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
private void setKneipenListe() {
locations= new Location("Hexenlochküche","Hexenloch 13","Restaurant",4);
totalLocations.add(locations);
locations= new Location("Cafe Mayerhofer","Marktplatz 16","Cafe",5);
totalLocations.add(locations);
locations= new Location("Bergstüble","Im Mäderstal 4","Restaurant",4);
totalLocations.add(locations);
locations= new Location("Fallenbengel","Allmendenstraße 1","Restaurant",3);
totalLocations.add(locations);
locations= new Location("Klimperkasten","Bahnhofstraße 70","Kneipe",2);
totalLocations.add(locations);
locations= new Location("Gaststätte Zur Tanne","Bismarkstraße 18","Gaststätte",2);
totalLocations.add(locations);
locations= new Location("Viva Cafe-Bar","Bismarkstraße 18","Kneipe", 4);
totalLocations.add(locations);
locations= new Location("Der Speicher","Am Großhausberg 9","Kneipe",5);
totalLocations.add(locations);
locations= new Location("Pizzaria Europa","Friedrichstraße 12","Pizzaria",4);
totalLocations.add(locations);
locations= new Location("Gasthaus Bad","Baumanstraße 26","Restaurant",5);
totalLocations.add(locations);
locations= new Location("Cafe Tannenhof","Oskar-Bürklestraße 2","Cafe",1);
totalLocations.add(locations);
locations= new Location("Waldhotel am Stausee","Preßwitzerstraße 5","Bar",2);
totalLocations.add(locations);
locations= new Location("Cafe Mayerhofer","Marktplatz 16","Cafe",5);
totalLocations.add(locations);
locations= new Location("Bergstüble","Im Mäderstal 4","Restaurant",4);
totalLocations.add(locations);
locations= new Location("Fallenbengel","Allmendenstraße 1","Restaurant",3);
totalLocations.add(locations);
locations= new Location("Klimperkasten","Bahnhofstraße 70","Kneipe",2);
totalLocations.add(locations);
locations= new Location("Gaststätte Zur Tanne","Bismarkstraße 18","Gaststätte",2);
totalLocations.add(locations);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment