在这种方法中,我收到了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中的另一个活动。当用户点击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);
答案 0 :(得分:4)
请参阅我的以下示例代码,将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 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
答案 1 :(得分:1)
您可以使用以下方式执行此操作
您可以将您单击的网格项(此处图像)的位置设置为imageview的标记,然后您可以使用上面的位置从数组列表中获取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();
}
});