单击网格视图时如何将数组列表(位置)发送到另一个活动

2024-04-19

在这种方法中我收到ArrayList

        OkHttpHandler handler = new OkHttpHandler(MainActivity.this,new OkHttpHandler.MyInterface() {
            @Override
            public void myMethod(ArrayList result) {
                                  Toast.makeText(MainActivity.this, "Connection Succesful",
                            Toast.LENGTH_LONG).show();

  GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), R.layout.grid_item_layout, result);

我想要的是将这个数组列表发送到 gridiview 中的另一个活动。当用户单击 gridview 中的图像时,我想将 ArrayList 中的该图像发送到下一个活动。如何做到这一点>

这是网格视图

holder.imageView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.d("OnImageButton", "Clicked");
        Intent intnt  =new Intent(mcontext, SingleViewActivity.class);
       //intnt.putExtra("Contact_list", item);
        mcontext.startActivity(intnt)  ; //This line raises error
        Toast.makeText(mcontext, "intent",
                Toast.LENGTH_LONG).show();
    }
});

我尝试了 parcable,但它不起作用,因为我只想将数据发送到另一个活动,我不想启动新活动

这就是我尝试过的

 Listitem = new ArrayList<Listitem>();
            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);
              //  String id ="2";
                String id=  c.getString("ID");
                String url = c.getString("URL");
                Log.d("Id: ", id);
                int intid = 0;
              Student student = new Student(c.getString("ID"),
                      "hhe",
                        c.getString("URL")
                       );
                Intent intent = new Intent(mContext,SingleViewActivity.class);

                // Passing data as a parecelable object to StudentViewActivity
                intent.putExtra("student",student);

                // Opening the activity
                mContext.startActivity(intent);

在这个方法中,我通过ArrayList到另一项活动

try {

        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray("result");
        System.out.println(peoples.length());

        Listitem = new ArrayList<Listitem>();
        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String id=  c.getString("ID");
            String url = c.getString("URL");
            Log.d("Id: ", id);
            int intid = 0;
            try {
                intid = Integer.parseInt(id.toString());
            } catch(NumberFormatException nfe) {
                System.out.println("Could not parse " + nfe);
            }
            DatabaseHandler db = new DatabaseHandler(mContext);
            Log.d("Insert: ", "Inserting ..");

              db.addObjects(new Objects(intid,"Image1", url, "IMAGES", "Funny"));

            Listitem.add(new Listitem(id,url));
            Log.e("d", "ppppp");
            System.out.println(Listitem);
        }
        if (mListener != null)
              mListener.myMethod(Listitem);

请参考我的以下示例代码,将数组列表发送到另一个活动,然后您可以将其逻辑用于您的应用程序。希望这可以帮助!

首先,你需要一个实现 Parcelable 的类

public class Person implements Parcelable {
    int id;
    String name;
    int age;

    Person (Parcel in){
        this.id = in.readInt();
        this.name = in.readString();
        this.age = in.readInt();
    }

    Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.name);
        dest.writeInt(this.age);
    }

    public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}

然后在主活动中:

public class MainActivity extends AppCompatActivity {

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

        ArrayList<Person> personArrayList = new ArrayList<>();
        personArrayList.add(new Person(1, "Person A", 20));
        personArrayList.add(new Person(2, "Person B", 30));
        personArrayList.add(new Person(3, "Person C", 40));

        Intent intent = new Intent(this,PersonsActivity.class);
        intent.putExtra("Person_List", personArrayList);
        startActivity(intent);
    }
}

人员活动:

public class PersonsActivity extends AppCompatActivity {

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

        Bundle bundle = getIntent().getExtras();

        ArrayList<Person> personArrayList = bundle.getParcelableArrayList("Person_List");

        if (personArrayList != null && !personArrayList.isEmpty()) {
            for (Person person : personArrayList) {
                Log.i("PersonsActivity", String.valueOf(person.id) + " | " + person.name + " | " + String.valueOf(person.age));
            }
        }
    }
}

您将得到以下 logcat:

11-23 15:40:37.107 4051-4051/? I/PersonsActivity: 1 | Person A | 20
11-23 15:40:37.107 4051-4051/? I/PersonsActivity: 2 | Person B | 30
11-23 15:40:37.107 4051-4051/? I/PersonsActivity: 3 | Person C | 40
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

单击网格视图时如何将数组列表(位置)发送到另一个活动 的相关文章

  • java.lang.IllegalStateException:密码未初始化

    我已经在 Android 应用程序中实现了加密 解密 我添加了一个加密类 该类已成为单例类 部分代码如下 public class Encryption private SecretKeySpec mKey null private Cip
  • 将图像从 Android 上传到 GCS

    我正在尝试将图像从 Android 直接上传到 Google 云存储 但API似乎不起作用 他们有一些与 App 引擎相关的 Java 示例 我没有看到任何被证明可以在 Android 上运行的示例 在 Android 上 我尝试使用 js
  • 找不到我的绑定的 inflate 方法(使用 Android,数据绑定。)

    我正在使用数据绑定来绑定 Android 应用程序中的布局 我已经设置了布局 my custom xml 并生成了绑定类 MyCustomBinding 但 Android Studio 似乎没有立即找到 Binding 类的 inflat
  • 虹膜和面部识别生物识别技术

    In blog Android P 中更好的生物识别技术 https android developers googleblog com 2018 06 better biometrics in android p html他们说 为了确保
  • 如何使用MonkeyDevice.instrument?

    嗨 大家好 我正在尝试从 MonkeyRunner 脚本运行我的测试仪器之一 不幸的是我无法让它工作 我尝试使用不同的参数变量调用 MonkeyDevice instrument 但没有成功 我试过了 设备 MonkeyRunner wai
  • 设置显式注释处理器

    我正在尝试将 Maven 存储库添加到我的 Android Studio 项目中 当我进行 Gradle 项目同步时 一切都很好 但是 每当我尝试构建我的 apk 时 都会收到此错误 Execution failed for task ap
  • 如何使用 Android 加速计?

    我正在尝试构建一个应用程序来读取手机上加速度计的值 该应用程序仅支持 Android 2 1 如何使用 2 1 兼容代码读取加速度计 从这个开始 public class yourActivity extends Activity impl
  • API 级别 15 的印地语字体(又名 Android 4.0.2)

    我有一个基于印地语内容的 Android 应用程序 并使用了 Android API 16 SDK 中的 devangiri 字体 并重命名为印地语 ttf 文本在 API 级别 16 和 17 上渲染良好 但在 Android API 级
  • 如何在屏幕上动态移动 Textview? (框架布局)

    我有一个应用程序 可以在屏幕上的 FrameLayout 上显示相机视图 屏幕处于固定风景模式 我需要编写一个带有动态确定的屏幕坐标的textView 坐标以百分比确定 例如 将文本视图写入屏幕坐标 x 80 y 20 屏幕上 将文本视图写
  • 如何在android中的google(设备)本机应用程序上添加自定义按钮?

    我想在谷歌 设备 的本机应用程序上添加一个按钮 例如 谷歌地图 使用此按钮我想打开我的应用程序 我已经做了一些相关工作 Using 无障碍服务 https developer android com reference android ac
  • Android SDK 中缺少 Gradle(使用 cordova + ionic)

    是的 我搜索了这个 但没有搜索到任何内容 只是有关 ANDROID HOME 路径的信息 但就我而言 我认为这不是真正的问题 当我尝试做的时候ionic build android我收到这个错误 错误 在 android sdk 中找不到
  • Android - GC 滞后于列表视图滚动“更大”的图像

    在列表视图中 我想在列表条目上绘制一个图像 这 20 张图像必须缩放以填充垂直模式的宽度 手机分辨率为 480 x 800 像素 SGS2 图像分辨率为 400x400 大小约为 100KB 我已将图像放在可绘制文件夹中 当我滚动列表时 它
  • Android 4.3 KeyStore - 尝试检索密钥时链== null

    下列的这个博客 http nelenkov blogspot de 2013 08 credential storage enhancements android 43 html 我使用此代码来创建和存储KeyPair在 Android 密
  • 原生编程对于移动开发有何优势?

    我需要为一家公司在一些主要的移动操作系统上开发应用程序 特别是 iOS Android 和 WP7 我最初计划为三种不同的操作系统编写三个独立的应用程序 每个应用程序都使用本机 SDK 然而 这样做有什么好处吗 有许多可用的跨平台工具 Se
  • Android - 获取所有可用存储的列表

    我正在从互联网将数据下载到我的应用程序中 如果我指定内部内存 Environment getExternalStorageDirectory 我可能会遇到 空间不足 的问题 SD卡安装地址总是因手机而异 所以我想允许用户选择他喜欢的位置 在
  • 在 Android 应用程序中读取 CSV 文件

    我正在开发一个概念验证应用程序 以便我可以在我正在制作的更大的应用程序中实现该功能 我对 Java 和 Android 开发有点陌生 但希望这个问题不会太简单或太复杂 基本上 我试图从 CSV 文件中读取字符串列表 并使其可用于在应用程序的
  • Android - 向 Android 的内置应用程序添加菜单项

    我想在 联系人 菜单中添加按钮或菜单项 是否可以 我使用 Android 2 2 和 Eclipse 3 6 1 不可以 内置 Android 应用程序不支持菜单中的意图选项 即您无法向其中添加任何内容
  • 为什么文件传输完成后我的列表视图条目的内容没有更新?

    为什么将文件复制到目录后listview没有更新驻留在该目录中的较新内容 仅当我退出时listview重新进入视图会刷新吗 有人可以告诉我如何解决这个问题吗 文件功能java文件的复制 您应该将新复制的文件 VideoInfo对象 添加到您
  • 如何在我的 Android 库 (AAR) 中包含 proguard 配置

    Android 库 根据AAR 文件规范 http tools android com tech docs new build system aar format 包含一个 proguard txt 文件 我的理解是 该文件声明了如何正确地
  • 如何消除按钮和其他视图之间的额外间隙?

    当我创建按钮视图时 Android 总是在该按钮与其下方的其他视图之间创建一些额外的空间 在下面的示例中 第二个按钮上方有一个按钮 您可以看到这两个按钮之间的间隙 我怎样才能摆脱这个差距 谢谢

随机推荐