Android - Passando informações entre Activities e ListView
De Aulas
Links relacionados: Dispositivos Móveis
Descrição
Este exemplo mostra como passar informações (parâmetros) entre Activities. Para tal, é necessário criar duas Activities, uma Basic chamada MainActivit.java/activity_main.xml/content_main.xml e uma Empty chamada EditActivity.java/activity_edit.xml.
content_mail.xml
1<?xml version="1.0" encoding="utf-8"?>
2<android.support.constraint.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 app:layout_behavior="@string/appbar_scrolling_view_behavior"
9 tools:context="com.saulopz.listview2activities.MainActivity"
10 tools:showIn="@layout/activity_main">
11
12 <ListView
13 android:id="@+id/list"
14 android:layout_width="368dp"
15 android:layout_height="495dp"
16 tools:layout_editor_absoluteX="8dp"
17 tools:layout_editor_absoluteY="8dp"/>
18</android.support.constraint.ConstraintLayout>
activity_edit.xml
1<?xml version="1.0" encoding="utf-8"?>
2<android.support.constraint.ConstraintLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:app="http://schemas.android.com/apk/res-auto"
5 xmlns:tools="http://schemas.android.com/tools"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 tools:context="com.saulopz.listview2activities.EditActivity">
9
10 <TextView
11 android:id="@+id/position"
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:layout_marginLeft="7dp"
15 android:layout_marginTop="25dp"
16 android:text="-1"
17 app:layout_constraintLeft_toLeftOf="parent"
18 app:layout_constraintTop_toTopOf="parent"/>
19
20 <EditText
21 android:id="@+id/name"
22 android:layout_width="342dp"
23 android:layout_height="38dp"
24 android:layout_marginLeft="8dp"
25 android:layout_marginRight="8dp"
26 android:layout_marginTop="16dp"
27 android:ems="10"
28 android:hint="Name"
29 android:inputType="textPersonName"
30 app:layout_constraintHorizontal_bias="0.243"
31 app:layout_constraintLeft_toRightOf="@+id/position"
32 app:layout_constraintRight_toRightOf="parent"
33 app:layout_constraintTop_toTopOf="parent"/>
34
35 <Button
36 android:id="@+id/button"
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content"
39 android:layout_marginLeft="8dp"
40 android:layout_marginRight="8dp"
41 android:layout_marginTop="70dp"
42 android:text="Ok"
43 app:layout_constraintLeft_toLeftOf="parent"
44 app:layout_constraintRight_toRightOf="parent"
45 app:layout_constraintTop_toBottomOf="@+id/name"/>
46</android.support.constraint.ConstraintLayout>
MainActivity.java
1package com.saulopz.listview2activities;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.support.design.widget.FloatingActionButton;
6import android.support.v7.app.AppCompatActivity;
7import android.support.v7.widget.Toolbar;
8import android.view.Menu;
9import android.view.MenuItem;
10import android.view.View;
11import android.widget.AdapterView;
12import android.widget.ArrayAdapter;
13import android.widget.ListView;
14
15import java.util.ArrayList;
16import java.util.Collections;
17import java.util.List;
18
19public class MainActivity extends AppCompatActivity {
20 private ListView list;
21 private ArrayAdapter<String> adapter;
22 private List<String> names = new ArrayList<>();
23
24 @Override
25 protected void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.activity_main);
28 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
29 setSupportActionBar(toolbar);
30
31 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
32 fab.setOnClickListener(new View.OnClickListener() {
33 @Override
34 public void onClick(View view) {
35 Intent intent = new Intent(MainActivity.this, EditActivity.class);
36 startActivityForResult(intent, 0);
37 }
38 });
39
40 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
41 android.R.id.text1, names);
42 list = (ListView) findViewById(R.id.list);
43 list.setAdapter(adapter);
44 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
45 public void onItemClick(AdapterView<?> parent, View view,
46 int position, long id) {
47 Intent intent = new Intent(MainActivity.this, EditActivity.class);
48 Bundle parms = new Bundle();
49 parms.putInt("position", position);
50 parms.putString("name", (String) parent.getItemAtPosition(position));
51 intent.putExtras(parms);
52 startActivityForResult(intent, 0);
53 }
54 });
55 list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
56 public boolean onItemLongClick(AdapterView<?> parent, View view,
57 int position, long id) {
58 names.remove(names.get(position));
59 adapter.notifyDataSetChanged();
60 return true;
61 }
62 });
63 }
64
65 @Override
66 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
67 if (resultCode == RESULT_OK) {
68 if (data.hasExtra("position") && data.hasExtra("name")) {
69 int position = data.getExtras().getInt("position");
70 String name = data.getExtras().getString("name");
71 if (position >= 0) {
72 names.remove(names.get(position));
73 }
74 names.add(name);
75 Collections.sort(names);
76 adapter.notifyDataSetChanged();
77 }
78 }
79 }
80
81 @Override
82 public boolean onCreateOptionsMenu(Menu menu) {
83 getMenuInflater().inflate(R.menu.menu_main, menu);
84 return true;
85 }
86
87 @Override
88 public boolean onOptionsItemSelected(MenuItem item) {
89 int id = item.getItemId();
90 if (id == R.id.action_settings) return true;
91 return super.onOptionsItemSelected(item);
92 }
93}
EditActivity.java
1package com.saulopz.listview2activities;
2
3import android.content.Intent;
4import android.support.v7.app.AppCompatActivity;
5import android.os.Bundle;
6import android.view.View;
7import android.widget.EditText;
8import android.widget.TextView;
9
10public class EditActivity extends AppCompatActivity implements View.OnClickListener {
11 private TextView position;
12 private EditText name;
13
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_edit);
18
19 position = (TextView) findViewById(R.id.position);
20 name = (EditText) findViewById(R.id.name);
21
22 Bundle args = getIntent().getExtras();
23 if (getIntent().hasExtra("position") && getIntent().hasExtra("name")) {
24 position.setText(String.valueOf(args.getInt("position")));
25 name.setText(args.getString("name"));
26 }
27 findViewById(R.id.button).setOnClickListener(this);
28 }
29
30 @Override
31 public void onClick(View v) {
32 if (v.getId() == R.id.button) {
33 if (!name.getText().toString().trim().equals("")) {
34 Intent data = new Intent();
35 Bundle parms = new Bundle();
36 parms.putInt("position", Integer.valueOf(position.getText().toString()));
37 parms.putString("name", name.getText().toString());
38 data.putExtras(parms);
39 setResult(RESULT_OK, data);
40 finish();
41 }
42 }
43 }
44}