[Android] Intent在Activity间传递数据

🌳程序效果

在这里插入图片描述
点击listview的item,跳转页面
在这里插入图片描述
点击确定则删除。


🌳代码展示

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/MyListView"/>

</LinearLayout>

activity_secondary.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondaryActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginLeft="20dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/confirm"
            android:layout_marginLeft="20dp"
            android:text="确定"
            android:textSize="20sp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cancel"
            android:text="取消"
            android:textSize="20sp"
            android:layout_marginLeft="20dp"/>

    </LinearLayout>

    
</LinearLayout>

MainActivity.java

package cn.edu.henu.lab_3_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static android.content.ContentValues.TAG;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    public List<String> list;
    private int index;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.MyListView);
        String[] people={"叶秋","黄少天","喜羊羊","小灰灰","王俊凯"};
        list=new ArrayList(Arrays.asList(people));
        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(MainActivity.this,SecondaryActivity.class);
                Log.d(TAG, "onItemClick: "+i);
                Log.d(TAG, "onItemClick: "+l);

                index=i;
                String info1="删除 "+listView.getItemAtPosition(i).toString()+" 的记录吗";
                intent.putExtra("sendinfo",info1);
                startActivityForResult(intent,1);
            }
        });

    }

    protected void onActivityResult(int requestCode,int resultCode,Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    list.remove(index);
                    adapter.notifyDataSetChanged();
                    Log.d(TAG, "onActivityResult: " + list.get(index));
                }
                break;
            default:
                break;
        }
    }

}

SecondaryActivity.java

package cn.edu.henu.lab_3_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SecondaryActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView info;
    private Button confirm;
    private Button cancel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary);

        info=findViewById(R.id.info);
        confirm=findViewById(R.id.confirm);
        cancel=findViewById(R.id.cancel);

        Intent intent=SecondaryActivity.this.getIntent();
        String info1=intent.getStringExtra("sendinfo");
        info.setText(info1);

        confirm.setOnClickListener(this);
        cancel.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.confirm:
                SecondaryActivity.this.setResult(RESULT_OK,SecondaryActivity.this.getIntent());
                break;
            case R.id.cancel:
                SecondaryActivity.this.setResult(RESULT_CANCELED,SecondaryActivity.this.getIntent());
                break;
            default:
                break;
        }
        SecondaryActivity.this.finish();
    }
}

SecondaryActivity.this.setResult(RESULT_CANCELED,SecondaryActivity.this.getIntent());
原本我是写做
SecondaryActivity.this.setResult(RESULT_CANCELED);
不带intent,因为没有携带数据,而我只需要一个返回的ResultCode就可以了。
正常写没有问题,程序可以跑起来。但是最后功能没有实现。所以最好还是带上intent。

如果要返回数据的话
SecondaryActivity.this.getIntent().putExtra("returninfo","你要返回的数据");

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开心星人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值