|
package com.example.user.arrayadapter1; |
|
|
|
import android.support.v7.app.AppCompatActivity; |
|
import android.os.Bundle; |
|
import android.view.View; |
|
import android.widget.AdapterView; |
|
import android.widget.ArrayAdapter; |
|
import android.widget.Button; |
|
import android.widget.EditText; |
|
import android.widget.ListView; |
|
import android.widget.TextView; |
|
import android.widget.Toast; |
|
|
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ |
|
private ArrayAdapter<Person> listAdapter; |
|
private ListView listView; |
|
private EditText editText; |
|
private ArrayList<Person> people; |
|
|
|
@Override |
|
protected void onCreate(Bundle savedInstanceState) { |
|
super.onCreate(savedInstanceState); |
|
setContentView(R.layout.activity_main); |
|
Button button = (Button) findViewById(R.id.addButton); |
|
button.setOnClickListener(this); |
|
|
|
// Creating a list of initialization data for the list view |
|
// We're using composite objects - Person |
|
people = new ArrayList(); |
|
people.add(new Person("Eli", 10)); |
|
people.add(new Person("Dani", 11)); |
|
people.add(new Person("Rani", 12)); |
|
|
|
// Creating an array adapter (INIT DATA IS SUPPLIED AT THE END OF THE LINE) |
|
// The title of each item in the listview, is decided by the value a Person's toString() method |
|
// The listview calls the toString() method of its items, and uses the returned string as |
|
// the name of the item in the list |
|
listAdapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, people); |
|
|
|
// Connecting the array adapter to the listview |
|
listView = (ListView) findViewById(R.id.listView); |
|
listView.setAdapter(listAdapter); |
|
|
|
// this.editText = (EditText) findViewById(R.id.editText1); |
|
|
|
// Registering the list view to a single short click event (callback) |
|
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
|
@Override |
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
|
Person person = (Person) parent.getItemAtPosition(position); |
|
Toast.makeText(getApplicationContext(), person.getName()+ " " + person.getAge(), Toast.LENGTH_SHORT).show(); |
|
} |
|
}); |
|
} |
|
|
|
@Override |
|
public void onClick(View view) { |
|
|
|
// Getitng a reference to the UI item where the user write names |
|
TextView textView = (TextView) findViewById(R.id.editText1); |
|
|
|
// Extracting the name the user wrote |
|
String name = textView.getText().toString(); |
|
|
|
// Creating a person (hard coded age - 20) |
|
Person person = new Person(name, 20); |
|
|
|
// Adding the user to the names list (ArrayList) |
|
people.add(person); |
|
|
|
// Clearing the area where the user writes |
|
textView.setText(""); |
|
|
|
// Updating the list (a classing Model - View interaction + Observer design pattern) |
|
listAdapter.notifyDataSetChanged(); |
|
} |
|
|
|
@Override |
|
public void onSaveInstanceState(Bundle state) { |
|
super.onSaveInstanceState(state); |
|
state.putSerializable("people", people); |
|
} |
|
|
|
@Override |
|
public void onRestoreInstanceState(Bundle state) { |
|
super.onRestoreInstanceState(state); |
|
this.people.clear(); |
|
this.people.addAll((ArrayList<Person>) state.getSerializable("people")); |
|
this.listAdapter.notifyDataSetChanged(); |
|
} |
|
} |
Great