Android - ListView e duas Activities
De Aulas
Links relacionados: Programação para Dispositivos Móveis
Interface
- Projeto tipo Empty Activity;
- Nova Empty Activity chamada ContactActivity;
strings.xml
1<resources>
2 <string name="app_name">Passagem</string>
3 <string name="name">Name</string>
4 <string name="phone">Phone</string>
5 <string name="save">Save</string>
6 <string name="remove">Remove</string>
7 <string name="close">Close</string>
8</resources>
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?>
2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 tools:context=".MainActivity">
8
9 <ListView
10 android:id="@+id/listView"
11 android:layout_width="368dp"
12 android:layout_height="440dp"
13 android:layout_marginStart="8dp"
14 android:layout_marginTop="8dp"
15 android:layout_marginEnd="8dp"
16 android:layout_marginBottom="8dp"
17 app:layout_constraintBottom_toBottomOf="parent"
18 app:layout_constraintEnd_toEndOf="parent"
19 app:layout_constraintStart_toStartOf="parent"
20 app:layout_constraintTop_toBottomOf="@+id/buttonNew" />
21
22 <Button
23 android:id="@+id/buttonNew"
24 android:layout_width="wrap_content"
25 android:layout_height="wrap_content"
26 android:layout_marginStart="8dp"
27 android:layout_marginTop="8dp"
28 android:layout_marginEnd="8dp"
29 android:text="new"
30 app:layout_constraintEnd_toEndOf="parent"
31 app:layout_constraintStart_toStartOf="parent"
32 app:layout_constraintTop_toTopOf="parent" />
33</android.support.constraint.ConstraintLayout>
activity_contact.xml
1<?xml version="1.0" encoding="utf-8"?>
2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 tools:context=".ContactActivity">
8
9 <EditText
10 android:id="@+id/editTextName"
11 android:layout_width="0dp"
12 android:layout_height="wrap_content"
13 android:layout_marginStart="8dp"
14 android:layout_marginTop="8dp"
15 android:layout_marginEnd="8dp"
16 android:ems="10"
17 android:hint="@string/name"
18 android:inputType="textPersonName"
19 app:layout_constraintEnd_toEndOf="parent"
20 app:layout_constraintStart_toStartOf="parent"
21 app:layout_constraintTop_toTopOf="parent" />
22
23 <EditText
24 android:id="@+id/editTextPhone"
25 android:layout_width="0dp"
26 android:layout_height="wrap_content"
27 android:layout_marginStart="8dp"
28 android:layout_marginTop="8dp"
29 android:layout_marginEnd="8dp"
30 android:ems="10"
31 android:hint="@string/phone"
32 android:inputType="textPersonName"
33 app:layout_constraintEnd_toEndOf="parent"
34 app:layout_constraintStart_toStartOf="parent"
35 app:layout_constraintTop_toBottomOf="@+id/editTextName" />
36
37 <Button
38 android:id="@+id/buttonSave"
39 android:layout_width="wrap_content"
40 android:layout_height="wrap_content"
41 android:layout_marginStart="8dp"
42 android:layout_marginTop="8dp"
43 android:text="@string/save"
44 app:layout_constraintStart_toStartOf="parent"
45 app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />
46
47 <Button
48 android:id="@+id/buttonRemove"
49 android:layout_width="wrap_content"
50 android:layout_height="wrap_content"
51 android:layout_marginStart="8dp"
52 android:layout_marginTop="8dp"
53 android:layout_marginEnd="8dp"
54 android:text="@string/remove"
55 app:layout_constraintEnd_toStartOf="@+id/buttonClose"
56 app:layout_constraintStart_toEndOf="@+id/buttonSave"
57 app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />
58
59 <Button
60 android:id="@+id/buttonClose"
61 android:layout_width="wrap_content"
62 android:layout_height="wrap_content"
63 android:layout_marginTop="8dp"
64 android:layout_marginEnd="8dp"
65 android:text="@string/close"
66 app:layout_constraintEnd_toEndOf="parent"
67 app:layout_constraintTop_toBottomOf="@+id/editTextPhone" />
68</android.support.constraint.ConstraintLayout>
Código Java
Contact.java
1package com.project.saulopz.myapplication;
2
3public class Contact {
4 private int id;
5 private String name;
6 private String phone;
7
8 public Contact() {
9 }
10
11 public Contact(int id, String name, String phone) {
12 setId(id);
13 setName(name);
14 setPhone(phone);
15 }
16
17 public int getId() {
18 return id;
19 }
20
21 public void setId(int id) {
22 this.id = id;
23 }
24
25 public String getName() {
26 return name;
27 }
28
29 public void setName(String name) {
30 this.name = name;
31 }
32
33 public String getPhone() {
34 return phone;
35 }
36
37 public void setPhone(String phone) {
38 this.phone = phone;
39 }
40
41 public String toString() {
42 return name;
43 }
44}
ContactActivity.java
1package com.example.saulo.passagem;
2
3import android.content.Intent;
4import android.support.v7.app.AppCompatActivity;
5import android.os.Bundle;
6import android.view.View;
7import android.widget.EditText;
8
9public class ContactActivity extends AppCompatActivity implements View.OnClickListener {
10 private int id = 0;
11 private EditText textName;
12 private EditText textPhone;
13
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_contact);
18
19 textName = findViewById(R.id.editTextName);
20 textPhone = findViewById(R.id.editTextPhone);
21
22 findViewById(R.id.buttonSave).setOnClickListener(this);
23 findViewById(R.id.buttonRemove).setOnClickListener(this);
24 findViewById(R.id.buttonClose).setOnClickListener(this);
25
26 Bundle args = getIntent().getExtras();
27 if (getIntent().hasExtra("id")) {
28 id = args.getInt("id");
29 textName.setText(args.getString("name"));
30 textPhone.setText(args.getString("phone"));
31 } else {
32 findViewById(R.id.buttonRemove).setVisibility(View.GONE);
33 }
34 }
35
36 @Override
37 public void onClick(View v) {
38 Intent data = new Intent();
39 Bundle parms = new Bundle();
40 switch (v.getId()) {
41 case R.id.buttonSave:
42 if (textName.getText().toString().equals("")) {
43 setResult(RESULT_CANCELED);
44 break;
45 }
46 parms.putInt("id", id);
47 parms.putString("name", textName.getText().toString());
48 parms.putString("phone", textPhone.getText().toString());
49 data.putExtras(parms);
50 setResult(RESULT_OK, data);
51 break;
52 case R.id.buttonRemove:
53 parms.putInt("id", id);
54 data.putExtras(parms);
55 setResult(RESULT_OK, data);
56 break;
57 case R.id.buttonClose:
58 setResult(RESULT_CANCELED);
59 break;
60 }
61 finish();
62 }
63}
MainActivity.java
1package com.example.saulo.passagem;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.support.v7.app.AppCompatActivity;
6import android.view.View;
7import android.widget.AdapterView;
8import android.widget.ArrayAdapter;
9import android.widget.Button;
10import android.widget.ListView;
11
12import java.util.ArrayList;
13import java.util.Comparator;
14
15public class MainActivity extends AppCompatActivity {
16 private ArrayList<Contact> list = new ArrayList<>();
17 private int maxId = 0;
18 private ArrayAdapter adapter;
19
20 @Override
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);
23 setContentView(R.layout.activity_main);
24
25 adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
26 final ListView listView = findViewById(R.id.listView);
27 listView.setAdapter(adapter);
28 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
29 @Override
30 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
31 Intent intent = new Intent(MainActivity.this, ContactActivity.class);
32 Bundle parms = new Bundle();
33 Contact c = (Contact) parent.getItemAtPosition(position);
34 parms.putInt("id", c.getId());
35 parms.putString("name", c.getName());
36 parms.putString("phone", c.getPhone());
37 intent.putExtras(parms);
38 startActivityForResult(intent, 0);
39 }
40 });
41
42 Button btNew = findViewById(R.id.buttonNew);
43 btNew.setOnClickListener(new View.OnClickListener() {
44 @Override
45 public void onClick(View v) {
46 Intent intent = new Intent(MainActivity.this, ContactActivity.class);
47 startActivityForResult(intent, 0);
48 }
49 });
50 }
51
52 @Override
53 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
54 if (resultCode == RESULT_OK) {
55 if (data.hasExtra("id") && data.hasExtra("name") && data.hasExtra("phone")) {
56 int id = data.getExtras().getInt("id");
57 String name = data.getExtras().getString("name");
58 String phone = data.getExtras().getString("phone");
59 if (id == 0) {
60 maxId++;
61 adapter.add(new Contact(maxId, name, phone));
62 sort();
63 } else {
64 for (Contact c : list) {
65 if (c.getId() == id) {
66 c.setName(name);
67 c.setPhone(phone);
68 sort();
69 adapter.notifyDataSetChanged();
70 break;
71 }
72 }
73 }
74 } else if (data.hasExtra("id")) {
75 int id = data.getExtras().getInt("id");
76 for (Contact c : list) {
77 if (c.getId() == id) {
78 list.remove(c);
79 sort();
80 adapter.notifyDataSetChanged();
81 break;
82 }
83 }
84 }
85 }
86 }
87
88 private void sort() {
89 adapter.sort(new Comparator<Contact>() {
90 @Override
91 public int compare(Contact a, Contact b) {
92 return a.getName().compareTo(b.getName());
93 }
94 });
95 }
96}