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

人走茶凉 提交于 2019-12-20 03:24:06

问题


In this method I am receiving the 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);

what I want is to send this arraylist to another activity in the gridiview. When the user clicks on the image in gridview, I want to send this image in ArrayList to next activity. How to do that>

this is the 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 didn't work because I only want to send the data to another activity, I don't to start a new activity

this is 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 am passing ArrayList to another 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);

回答1:


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<Person> CREATOR = new Parcelable.Creator<Person>() {
        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<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 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<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));
            }
        }
    }
}

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



回答2:


You can do this using following way

You can set position of a grid item(here image) you click as a tag to imageview and then you can get the json object or single object from array list using above position and can send to another activity.

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



回答3:


//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..!


来源:https://stackoverflow.com/questions/33866314/when-clicked-on-grid-view-how-to-send-arralistposition-to-another-actvity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!