When I clicked on the grid screen, how to send an arralist (position) to another activity

In this method, I get 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);

I want to send this arraylist to another activity in gridiview. When a user clicks on an image in a gridview, I want to send this image to an ArrayList for the next action. How to do it>

this is a grid view

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();
    }
});

I tried parcable, but it did not work, because I only want to send data to another action, I should not start a new activity

that's what i tried

 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);

In this method, I pass ArrayListanother activity

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);
+2
source share
3 answers

, arraylist , . , !

, , 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];
        }
    };
}

MainActivity:

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);
    }
}

The PersonActivity:

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
+4

,

( ), , json , , .

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

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //Get your imageview here by using v.findviewbyid and get tag tag 
        //Like this
        //ImageView iv=(ImageView)v.findviewbyid(id of layout you mention to bind holder.imageView) 
         //Integer mPosition=(Integer)iv.getTag();
         //Then fetch that single object by using mPosition from your list and pass it  
        //JSONObject item = peoples.getJSONObject(mPosition);
        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();
    }
});
+1
//Make constant class and add all data in this arraylist:
//e.g : Constant.arrylist.add(<collection>);
public class Constant{
public static ArrayList<collection>() arrylist = new ArrayList<collection>()
}

//Pass arraylist data
Intent intent = new Intent(this,YourActivity.class);
intent.putInt("position", arrylist.get(position));
startActivity(intent);

//Get position in another activity
Bundle bundle = getIntent().getExtras();
int position = bundle.getInt("position",0);

//Now get Particular data 
//e.g
String url = Constant.arrylist.get(position).<url(collection)>;
//And so on..!
+1

Source: https://habr.com/ru/post/1615947/


All Articles