gpt4 book ai didi

android - 单击 GridView 时如何将数组列表(位置)发送到另一个 Activity

转载 作者:太空狗 更新时间:2023-10-29 16:14:23 25 4
gpt4 key购买 nike

在这个方法中,我收到了 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);

我想要的是将这个 arraylist 发送到 gridiview 中的另一个 Activity 。当用户单击 gridview 中的图像时,我想将 ArrayList 中的此图像发送到下一个 Activity 。 怎么做>

这是 GridView

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,但它没有用,因为我只想将数据发送到另一个 Activity ,我不想开始一个新 Activity

这是我试过的

 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 传递给另一个 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);

最佳答案

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

首先,你需要一个实现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);
}
}

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

您将获得以下 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

关于android - 单击 GridView 时如何将数组列表(位置)发送到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33866314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com