Преглед изворни кода

redesign the structure

master
Tobias Link пре 5 година
родитељ
комит
9405acbd18
5 измењених фајлова са 156 додато и 47 уклоњено
  1. +79
    -0
      app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/DrinkAdapter.java
  2. +35
    -44
      app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/MainActivity.java
  3. +37
    -0
      app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/Student.java
  4. +2
    -0
      app/src/main/res/layout/activity_main.xml
  5. +3
    -3
      app/src/main/res/layout/listview_activity.xml

+ 79
- 0
app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/DrinkAdapter.java Прегледај датотеку

@@ -0,0 +1,79 @@
package at.ac.perg.htl.bhif16.coffeeapp;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.ArrayList;


public class DrinkAdapter extends ArrayAdapter<Student> {

Context context;
int layoutResourceId;
int i;
ArrayList<Student> students = new ArrayList<Student>();

public DrinkAdapter(Context context, int layoutResourceId,
ArrayList<Student> studs) {
super(context, layoutResourceId, studs);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.students = studs;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
StudentWrapper StudentWrapper = null;

if (item == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
item = inflater.inflate(layoutResourceId, parent, false);
StudentWrapper = new StudentWrapper();
StudentWrapper.name = (TextView) item.findViewById(R.id.listview_item_title);
StudentWrapper.consumedDrinks = (TextView) item.findViewById(R.id.count_field);
StudentWrapper.img = (ImageView) item.findViewById(R.id.listview_image);
StudentWrapper.count = (Button) item.findViewById(R.id.count_btn);
item.setTag(StudentWrapper);
} else {
StudentWrapper = (StudentWrapper) item.getTag();
}

final Student student = students.get(position);
StudentWrapper.name.setText(student.getName());
StudentWrapper.consumedDrinks.setId(student.getConsumedDrinks());
StudentWrapper.img.setImageResource(student.getImage());


final DrinkAdapter.StudentWrapper finalStudentWrapper = StudentWrapper;
StudentWrapper.count.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

finalStudentWrapper.consumedDrinks.setText(student.getConsumedDrinks()+"");
student.setConsumedDrinks(student.getConsumedDrinks()+1);
}
});

return item;

}

static class StudentWrapper {
TextView name;
TextView consumedDrinks;
ImageView img;
Button count;

}

}

+ 35
- 44
app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/MainActivity.java Прегледај датотеку

@@ -1,40 +1,39 @@
package at.ac.perg.htl.bhif16.coffeeapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {


// Array of strings for ListView Title
String[] listviewTitle = new String[]{ "Achleitner", "Aichinger", "Bodingbauer",
"Bogdan","Derntl","Diesenreither",
"Forthofer","Fröschl","Fürholzer",
"Grünzweil","Gürler","Hametner",
"Hemmelmayr","Kargl","Link",
"Mayrhofer","Moser","Mühlbachler",
"Nerat","Pilgersdorfer","Rauch",
"Rauscher","Scheuchenpflug","Wall"};
public class MainActivity extends Activity {

int[] listviewImage = new int[]{ R.drawable.achleitner, R.drawable.aichinger, R.drawable.bodingbauer,
R.drawable.bogdan, R.drawable.derntl, R.drawable.unknown,
R.drawable.forthofer, R.drawable.froeschl, R.drawable.fuerholzer,
R.drawable.unknown, R.drawable.unknown, R.drawable.hametner,
R.drawable.hemmelmayr, R.drawable.kargl, R.drawable.link,
R.drawable.mayrhofer, R.drawable.moser, R.drawable.muehlbacher,
R.drawable.unknown, R.drawable.pilgersdorfer, R.drawable.rauch,
R.drawable.rauscher, R.drawable.scheuchenpflug, R.drawable.wall};

TextView count_field;
ListView listview;
DrinkAdapter studentArrayAdapter;
ArrayList<Student> studentArray = new ArrayList<Student>();


@Override
@@ -42,33 +41,25 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < listviewTitle.length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("listview_title", listviewTitle[i]);
hm.put("listview_image", Integer.toString(listviewImage[i]));
aList.add(hm);
}

String[] from = {"listview_image", "listview_title", "listview_discription"};
int[] to = {R.id.listview_image, R.id.listview_item_title};

SimpleAdapter simpleAdapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_activity, from, to);
ListView androidListView = (ListView) findViewById(R.id.list_view);
androidListView.setAdapter(simpleAdapter);






//TODO
//add students
studentArray.add(new Student("Achleitner",0,R.drawable.achleitner));
studentArray.add(new Student("Aichinger",0,R.drawable.aichinger));
studentArray.add(new Student("Bodingbauer",0,R.drawable.bodingbauer));
studentArray.add(new Student("Bogdan",0,R.drawable.bogdan));
studentArray.add(new Student("Derntl",0,R.drawable.derntl));
studentArray.add(new Student("Diesenreiter",0,R.drawable.unknown));
studentArray.add(new Student("Forthofer",0,R.drawable.forthofer));

studentArrayAdapter = new DrinkAdapter(MainActivity.this, R.layout.listview_activity, studentArray);
listview= (ListView) findViewById(R.id.list_view);
listview.setItemsCanFocus(false);
listview.setAdapter(studentArrayAdapter);



}


}


}

+ 37
- 0
app/src/main/java/at/ac/perg/htl/bhif16/coffeeapp/Student.java Прегледај датотеку

@@ -0,0 +1,37 @@
package at.ac.perg.htl.bhif16.coffeeapp;

public class Student {
private String name;
private int img;
private int consumedDrinks;

public Student(String name, int consumedDrinks, int img) {

this.name = name;
this.consumedDrinks = consumedDrinks;
this.img = img;

}

public int getImage() {
return img;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public int getConsumedDrinks() {
return consumedDrinks;
}

public void setConsumedDrinks(int consumedDrinks) {
this.consumedDrinks = consumedDrinks;
}
}

+ 2
- 0
app/src/main/res/layout/activity_main.xml Прегледај датотеку

@@ -6,8 +6,10 @@
android:orientation="vertical"
tools:context=".MainActivity">


<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>

+ 3
- 3
app/src/main/res/layout/listview_activity.xml Прегледај датотеку

@@ -31,13 +31,13 @@
android:text="+ Getränk"/>

<TextView
android:id="@+id/count_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/count_field"
android:gravity="center"
android:text="0"
android:textSize="15sp"
android:textStyle="bold"
android:text="1"/>
android:textStyle="bold" />


</LinearLayout>

Loading…
Откажи
Сачувај