ListView Studenten FULL
menu.xml
1) anlegen eines Ordners „menu“
2) file new XML Layout-XML-File
String.xml
ListView_Studenten
Status-Grafik
1. Semester
2. Semester
3. Semester
4. Semester
5. Semester
6. Semester
Bachelorarbeit
Neuer Eintrag
Bearbeiten oder Löschen eines Eintrags
Name
Matrnr
Semester
Note
Löschen
Neu
Löschen
Neu
Speichern
Abbrechen
- 1. Semester
- 2. Semester
- 3. Semester
- 4. Semester
- 5. Semester
- 6. Semester
- Bachelorarbeit
Color.xml
#3F51B5
#303F9F
#FF4081
#0000FF
#FF0000
activity_main.xml
rowlayout.xml
Activity_Edit_delete_new.xml
New Activity EmptyActivity
onclick
package com.example.kraft_300.testlistview_studenten;
import android.app.*;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.*;
import java.text.DecimalFormat;
// oberer Balken mit AppCompatActivity !!!!!!!!!!
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.*;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import android.util.Log;
import android.content.*;
import android.net.Uri;
import android.app.Activity;
// mit AppCompatActivity statt Activity
// hat man eine breitere Kopfzeile inkl. Menü 3 punkte !
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener{
// , AdapterView.OnItemLongClickListener
private static final String TAG = MainActivity.class.getSimpleName();
private ListView listView = null;
private StudentenAdapter adapter = null;
// hier fehlt Code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
Studentenliste.getInstance().loadInput(this,Const.FILENAME);
adapter = new StudentenAdapter(this);
listView.setAdapter(adapter);
Studentenliste.getInstance().saveInput(this,Const.FILENAME);
listView.setOnItemClickListener(this);
Log.i(TAG, "********************************* onCreate"); // import android.util.Log;
}
@Override
protected void onStart() {
super.onStart();
ActionBar bar = getActionBar();
if (bar!=null) {
bar.setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// hier fehlt Code
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
boolean res = super.onCreateOptionsMenu(menu);
// hier fehlt Code
return res;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_new_item) {
//Toast.makeText(this, "new angeklickt: "+item.getItemId(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,
activity_edit_delete_new.class);
//viewIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivityForResult(viewIntent);
intent.putExtra("ACTION",
activity_edit_delete_new.ACTION_NEW); // Hash value
startActivityForResult(intent,1);
return true;
}
// hier fehlt Code
return true;
}
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
// hole aktuellen Student
Student std = (Student) adapter.getItem(position);
int i=3;
switch (i) {
case 1:
Toast.makeText(MainActivity.this,
Integer.toString(position), Toast.LENGTH_LONG).show();
break;
case 2:
showURL(std);
break;
case 3:
// hier fehlt Code
}
//return true;
}
private void showEditDeleteNew(Student std, int position) {
//Anzeige des neuen View EditDeleteNew
Intent viewintent = new Intent(this,activity_edit_delete_new.class);
// die neuen Activity soll beim Ende zurückspringen
// callBack-Methode
viewintent.putExtra("ACTION",activity_edit_delete_new.ACTION_EDIT_DELETE);
viewintent.putExtra(Student.STDNAME,std.name); // STDNAME ist hashkey
viewintent.putExtra(Student.STDMATRNR,Integer.toString(std.matrnr));
viewintent.putExtra(Student.STDSEMESTER,Integer.toString(std.semester));
viewintent.putExtra(Student.STDNOTE,Double.toString(std.note));
viewintent.putExtra(Student.STDINDEX,Integer.toString(position));
if (std.select)
viewintent.putExtra(Student.STDSELECT,"ja");
else
viewintent.putExtra(Student.STDSELECT,"nein");
startActivityForResult(viewintent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
// import android.app.Activity;
if(resultCode == Activity.RESULT_OK){
// neu laden
Studentenliste.getInstance().saveInput(
this,Const.FILENAME);
adapter.notifyDataSetChanged();
Toast.makeText(this, "save gedrückt: ",Toast.LENGTH_LONG).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
private void showURL(Student std) {
String url = "http://www.miwilhelm.de/"+std.semester+"/";
// nun eine Webseite anzeigen
Intent viewintent =
new Intent("android.intent.action.VIEW", Uri.parse(url));
startActivity(viewintent);
}
}
public class Const {
public static String FILENAME = "studenten.bin";
}
package com.example.vorllistview2;
import android.content.Context;
import android.util.Log;
import java.io.*;
public class Student {
private static final String TAG = MainActivity.class.getSimpleName();
public static final String STDNAME = "STDNAME";
public static final String STDMATRNR = "STDMATRNR";
public static final String STDSEMESTER = "STDSEMESTER";
public static final String STDNOTE = "STDNOTE";
public static final String STDINDEX = "STDINDEX";
public static final String STDSELECT = "STDSELECT";
public String name="-";
public int matrnr = 1;
public int semester = 1;
public double note=1.0;
public boolean select=false;
public Student() {
}
public Student(String name, int matrnr, int semester, double note) {
this.name=name;
this.matrnr = matrnr;
this.semester=semester;
this.note = note;
}
public void init(Student std) {
this.name=std.name;
this.matrnr = std.matrnr;
this.semester=std.semester;
this.note = std.note;
}
// localization
public String getName(Context context) {
switch (semester) {
case 1:
return context.getString(R.string.sem1);
case 2:
return context.getString(R.string.sem2);
case 3:
return context.getString(R.string.sem3);
case 4:
return context.getString(R.string.sem4);
case 5:
return context.getString(R.string.sem5);
case 6:
return context.getString(R.string.sem6);
case 7:
return context.getString(R.string.bachelor);
default:
return context.getString(R.string.sem1);
}
}
public int getIdForDrawable() {
switch (semester) {
case 1:
return R.drawable.sem1;
case 2:
return R.drawable.sem2;
case 3:
return R.drawable.sem3;
case 4:
return R.drawable.sem4;
case 5:
return R.drawable.sem5;
case 6:
return R.drawable.sem6;
case 7:
return R.drawable.bachelor;
default:
return R.drawable.sem1;
}
} // getIdForDrawable
public void write2File(DataOutputStream dout) {
try {
dout.writeUTF(name); // Erst die Anzahl, dann die Zeichen
dout.writeInt(matrnr);
dout.writeDouble(note);
dout.writeInt(semester);
}
catch (IOException e) {
Log.e(TAG,"************* write2File error",e);
// oder ein Toast!
}
}
public void loadfromFile(DataInputStream din) {
try {
name=din.readUTF(); // Erst die Anzahl, dann die Zeichen
matrnr=din.readInt();
note=din.readDouble();
semester=din.readInt();
}
catch (IOException e) {
Log.e(TAG,"************* loadFromFile error",e);
}
}
}
import android.content.Context;
import android.util.Log;
import java.io.*;
import java.util.*;
public class Studentenliste {
private static final String TAG = MainActivity.class.getSimpleName();
public ArrayList liste = new ArrayList();
// Design Pattern Singleton
private static Studentenliste instance = new Studentenliste();
public static Studentenliste getInstance() {
return instance;
}
private Studentenliste() {
boolean ok=false;
if (ok) {
insertStudenten();
}
else {
}
}
private boolean FileExits(Context context, String filename) {
try {
FileInputStream fin = context.openFileInput(filename);
fin.close();
return true;
}
catch (IOException e){
return false;
}
}
public void loadInput(Context context, String filename) {
try {
if (FileExits(context, filename)) {
FileInputStream fin = context.openFileInput(filename);
DataInputStream din = new DataInputStream(fin);
int n = din.readInt(); // Anzahl der Studenten in der Datei
liste.clear();
for (int i=0; i=0; i--) {
if (liste.get(i).select) {
liste.remove(i);
}
}
}
public void createnewItems() {
insertStudenten();
}
private void insertStudenten() {
liste.clear();
liste.add(new Student("Meier, Anton", 12345, 7, 1.3));
liste.add(new Student("Schulze, Susanne", 12365, 6, 1.7));
liste.add(new Student("Brandt, Hugo", 22345, 6, 2.7));
liste.add(new Student("Binder, Gabi", 22365, 6, 4.0));
liste.add(new Student("Märtens, Friedrich", 22845, 5, 3.0));
liste.add(new Student("Müller, Andreas", 22349, 5, 1.0));
liste.add(new Student("Baumann, Herbert", 20445, 4, 2.0));
liste.add(new Student("Künneth, Thomas", 324455, 4, 3.7));
liste.add(new Student("Unbereit, Peter", 324355, 4, 3.3));
liste.add(new Student("Meyer, Hubert ", 324355, 3, 2.3));
liste.add(new Student("Altmeier, Jutta", 324355, 3, 5.0));
liste.add(new Student("Gisin, Nicoletta", 33445, 2, 2.7));
liste.add(new Student("Telemann, Dieter", 34645, 2, 4.0));
liste.add(new Student("Wagner, Hans", 38345, 2, 1.7));
liste.add(new Student("Wolff, Bernd", 39345, 1, 3.7));
liste.add(new Student("Prandtl, Daria", 40345, 1, 3.0));
liste.add(new Student("Burton, Michael", 41445, 1, 2.3));
liste.add(new Student("Fehr, Hauke", 42445, 1, 1.7));
liste.add(new Student("Klenke, Mario", 44445, 1, 5.0));
}
public void saveInput(Context context, String filename) {
try {
FileOutputStream fout = context.openFileOutput(filename, Context.MODE_PRIVATE);
DataOutputStream dout = new DataOutputStream(fout);
dout.writeInt(liste.size());
for (Student std : liste) {
std.write2File(dout);
}
fout.close();
}
catch (IOException e){
}
}
public void changeStudent(Student stdnew, int index) {
if (index>=0 && index=0 && index liste = null;
private final LayoutInflater inflator;
public StudentenAdapter(Context context) {
inflator = LayoutInflater.from(context);
liste = Studentenliste.getInstance().liste;
//Studentenliste.getInstance().saveInput(context,Const.FILENAME);
}
@Override
public int getCount() {
return liste.size();
}
@Override
public Object getItem(int position) {
return liste.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder {
public ImageView icon;
public TextView name;
public TextView matrnr;
public TextView note;
public TextView select;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// ist Zelle schon im Speicher
if (convertView==null) {
// Aufbau einen kleines Miniview für die zelle
convertView = inflator.inflate(R.layout.rowlayout, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.name = (TextView) convertView.findViewById(R.id.tv_name);
holder.matrnr = (TextView) convertView.findViewById(R.id.tv_matrnr);
holder.note = (TextView) convertView.findViewById(R.id.tv_note);
holder.select = (TextView) convertView.findViewById(R.id.tv_select);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
// Füllen mit den aktuellen Werten
Context context = parent.getContext();
Student std = (Student) getItem(position);
holder.icon.setImageResource(std.getIdForDrawable());
holder.name.setText(std.name);
holder.matrnr.setText(Integer.toString(std.matrnr));
holder.note.setText(Double.toString(std.note)); // decimalformat
if (std.select)
holder.select.setText("x ");
else
holder.select.setText("");
//position = (++position) % getCount();
return convertView;
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.*;
import android.widget.*;
import java.io.Console;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Pattern;
import android.text.*; // onChangeEvent
import android.view.*;
import android.util.Log;
import android.app.Activity;
public class activity_edit_delete_new extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
public static final String ACTION_EDIT_DELETE="1";
public static final String ACTION_EDIT="2";
public static final String ACTION_DELETE="3";
public static final String ACTION_NEW="4";
public String action = ACTION_EDIT_DELETE;
private TextView tvCaption = null;
private EditText edName=null;
private EditText edMatrnr=null;
private Spinner spSemester = null;
private Spinner spNote = null;
private EditText edSelect=null;
private Button bnSaveChanges = null;
private Button bnDelete = null;
private Button bnCancel = null;
// onChangeEvent
private Pattern namePattern =
Pattern.compile("^([A-ZÜÄÖ][a-züäöß]{1,29})[,][ ]([A-ZÜÄÖ][a-züäöß]{1,29})$");
private Pattern matrnrPattern =
Pattern.compile("^[1-9][0-9]{4}$");
private boolean isNameOk=false;
private boolean isMatnrOk=false;
private TextView tvValidStdname = null;
private TextView validMatrnr = null;
private int stdindex=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_delete_new);
tvCaption = (TextView) findViewById(R.id.tv_caption);
edName = (EditText) findViewById(R.id.ed_stdname);
edMatrnr = (EditText) findViewById(R.id.ed_stdmatrnr);
spSemester = (Spinner) findViewById(R.id.sp_semester);
spNote = (Spinner) findViewById(R.id.sp_noten);
edSelect = (EditText) findViewById(R.id.ed_stdselect);
bnSaveChanges = (Button) findViewById(R.id.bnSaveChanges);
bnDelete = (Button) findViewById(R.id.bnDelete);
bnCancel = (Button) findViewById(R.id.bnCancel);
Intent intent = getIntent();
List list = new ArrayList();
list.add("1.0");
list.add("1.3");
list.add("1.7");
list.add("2.0");
list.add("2.3");
list.add("2.7");
list.add("3.0");
list.add("3.3");
list.add("3.7");
list.add("4.0");
list.add("5.0");
ArrayAdapter dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spNote.setAdapter(dataAdapter);
// Abfrage auf Edit,Delete ODER New
action = intent.getStringExtra("ACTION");
if (action.equals(ACTION_EDIT_DELETE)) {
tvCaption.setText( getString(R.string.edit_delete_student) );
edName.setText(intent.getStringExtra(Student.STDNAME));
edMatrnr.setText(intent.getStringExtra(Student.STDMATRNR));
edSelect.setText(intent.getStringExtra(Student.STDSELECT));
// setzen des Semesters
String dummy = intent.getStringExtra(Student.STDSEMESTER);
int semesterIndex = 0;
try {
semesterIndex = Integer.parseInt(dummy) - 1;
} catch (NumberFormatException e) {
Toast.makeText(this,
"NumberFormatException: semesterIndex: "+dummy,
Toast.LENGTH_SHORT).show();
semesterIndex = 0;
}
spSemester.setSelection(semesterIndex);
// Note sendet
dummy = intent.getStringExtra(Student.STDNOTE); // 1.0 1.3 1.7
int indexNote = 0;
switch (dummy) {
case "1.0":
indexNote = 0;
break;
case "1.3":
indexNote = 1;
break;
case "1.7":
indexNote = 2;
break;
case "2.0":
indexNote = 3;
break;
case "2.3":
indexNote = 4;
break;
case "2.7":
indexNote = 5;
break;
case "3.0":
indexNote = 6;
break;
case "3.3":
indexNote = 7;
break;
case "3.7":
indexNote = 8;
break;
case "4.0":
indexNote = 9;
break;
case "5.0":
indexNote = 10;
break;
}
spNote.setSelection(indexNote);
this.isNameOk=true;
this.isMatnrOk=true;
dummy = intent.getStringExtra(Student.STDINDEX);
stdindex = -1;
try {
stdindex = Integer.parseInt(dummy);
} catch (NumberFormatException e) {
Toast.makeText(this,
"NumberFormatException: stdindex: "+dummy,
Toast.LENGTH_SHORT).show();
stdindex = -1;
}
}
else {
// new Student
//stdindex = -1;
tvCaption.setText( getString(R.string.new_student) );
// View.INVISIBLE View.VISIBLE View.GONE
bnDelete.setVisibility(View.GONE); // import android.view.*;
bnSaveChanges.setText( getString(R.string.bn_new));
spSemester.setSelection(0);
spNote.setSelection(0);
this.isNameOk=false;
this.isMatnrOk=false;
} // if (action.equals(ACTION_EDIT_DELETE)) {
setActionListeners();
} // create
// onChangeEvent für Name´und Matrnr
// wenn falsch, disabled saveButton
private void setActionListeners() {
edName.addTextChangedListener(new TextWatcher() {
private boolean isValid(CharSequence s) {
return namePattern.matcher(s).matches();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (isValid(s)) {
//tvValidStdname.setText("");
isNameOk = true;
}
else {
//tvValidStdname.setText("*");
isNameOk=false;
}
Log.e("*********************isNameOk",
Boolean.toString(isNameOk));
setEnabledbnOk();
}
});
edMatrnr.addTextChangedListener(new TextWatcher() {
private boolean isValid(CharSequence s) {
return matrnrPattern.matcher(s).matches();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (isValid(s)) {
//validMatrnr.setText("");
isMatnrOk = true;
}
else {
//validMatrnr.setText("*");
isMatnrOk = true;
}
setEnabledbnOk();
}
});
}
private void setEnabledbnOk() {
bnSaveChanges.setEnabled(this.isNameOk && this.isMatnrOk);
}
private Student checkInputs() {
String dummy;
String name;
int matrnr;
int semester;
double note=1.0;
name = edName.getText().toString();
try {
matrnr = Integer.parseInt(edMatrnr.getText().toString());
}
catch (NumberFormatException e) {
Toast.makeText(this, "wrong matrikelnumber", Toast.LENGTH_SHORT).show();
return null;
}
semester = ((int) spSemester.getSelectedItemPosition())+1;
int indexNote = (int) spNote.getSelectedItemPosition();
switch (indexNote) {
case 0:
note =1.0;
break;
case 1:
note =1.3;
break;
case 2:
note =1.7;
break;
case 3:
note =2.0;
break;
case 4:
note =2.3;
break;
case 5:
note =2.7;
break;
case 6:
note =3.0;
break;
case 7:
note =3.3;
break;
case 8:
note =3.7;
break;
case 9:
note =4.0;
break;
case 10:
note =5.0;
break;
default:
note =5.0;
break;
}
Student std = new Student();
std.name=name;
std.matrnr=matrnr;
std.semester=semester;
std.note=note;
return std;
} // CheckInputs
public void bnSaveChanges_click(View view) {
if (action.equals(ACTION_EDIT_DELETE)) {
Student std = checkInputs();
if (std == null) {
// es gab einen Fehler bei der Eingabe
Toast.makeText(this,
"Fehler beim Check",
Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this,
"stdindex: "+stdindex,Toast.LENGTH_SHORT).show();
Studentenliste stdliste = Studentenliste.getInstance();
stdliste.changeStudent(std, stdindex);
}
else {
Student std = checkInputs();
if (std == null) {
Toast.makeText(this,
"Fehler beim Check",
Toast.LENGTH_SHORT).show();
return;
}
Studentenliste stdliste = Studentenliste.getInstance();
stdliste.newStudent(std);
}
Intent returnIntent = new Intent();
returnIntent.putExtra("result", "1111");
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
public void bnCancel_click(View view) {
Intent returnIntent = new Intent();
// import android.app.Activity;
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
public void bnDelete_click(View view) {
Studentenliste stdliste = Studentenliste.getInstance();
stdliste.deleteStudent(stdindex);
Intent returnIntent = new Intent();
returnIntent.putExtra("result", "2222");
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}
23