When clicked on grid view how to send arralist(position) to another actvity

后端 未结 3 1065
不思量自难忘°
不思量自难忘° 2021-01-22 07:27

In this method I am receiving the ArrayList

        OkHttpHandler handler = new OkHttpHandler(MainActivity.this,new OkHttpHandler.MyInterface() {
           


        
3条回答
  •  一个人的身影
    2021-01-22 08:19

    Please refer to my following sample code for sending an arraylist to another activity, then you can use its logic to your app. Hope this helps!

    First of all, you need a class that implements 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 CREATOR = new Parcelable.Creator() {
            public Person createFromParcel(Parcel in) {
                return new Person(in);
            }
    
            public Person[] newArray(int size) {
                return new Person[size];
            }
        };
    }
    

    Then in MainActivity:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ArrayList 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);
        }
    }
    

    The PersonsActivity:

    public class PersonsActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_persons);
    
            Bundle bundle = getIntent().getExtras();
    
            ArrayList 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));
                }
            }
        }
    }
    

    You will get the following 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
    

提交回复
热议问题