loc*_*boy 79 android arraylist android-intent
我试图使用意图将arrayList传递给另一个活动.这是第一个活动中的代码.
case R.id.editButton:
Toast.makeText(this, "edit was clicked", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, editList.class);
intent.putStringArrayListExtra("stock_list", stock_list);
startActivity(intent);
break;
Run Code Online (Sandbox Code Playgroud)
这是我尝试在第二个活动中检索列表的地方.这里有问题吗?
Intent i = new Intent(); //This should be getIntent();
stock_list = new ArrayList<String>();
stock_list = i.getStringArrayListExtra("stock_list");
Run Code Online (Sandbox Code Playgroud)
Phi*_*lio 102
在您的接收意图中,您需要做:
Intent i = getIntent();
stock_list = i.getStringArrayListExtra("stock_list");
Run Code Online (Sandbox Code Playgroud)
你拥有它的方式你刚刚创建了一个新的空意图而没有任何额外的东西.
如果你只有一个额外的,你可以将其浓缩为:
stock_list = getIntent().getStringArrayListExtra("stock_list");
Run Code Online (Sandbox Code Playgroud)
Hir*_*tel 20
我通过以String形式传递ArrayList来完成这个 .
添加compile 'com.google.code.gson:gson:2.2.4'
在依赖块的build.gradle.
点击上同步项目与文件摇篮
Cars.java:
public class Cars {
public String id, name;
}
Run Code Online (Sandbox Code Playgroud)
当你想给通过 的ArrayList:
List<Cars> cars= new ArrayList<Cars>();
cars.add(getCarModel("1", "A"));
cars.add(getCarModel("2", "B"));
cars.add(getCarModel("3", "C"));
cars.add(getCarModel("4", "D"));
Gson gson = new Gson();
String jsonCars = gson.toJson(cars);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("list_as_string", jsonCars);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
按功能获取CarsModel:
private Cars getCarModel(String id, String name){
Cars cars = new Cars();
cars.id = id;
cars.name = name;
return cars;
}
Run Code Online (Sandbox Code Playgroud)
你必须导入java.lang.reflect.Type
;
在onCreate()上检索ArrayList:
String carListAsString = getIntent().getStringExtra("list_as_string");
Gson gson = new Gson();
Type type = new TypeToken<List<Cars>>(){}.getType();
List<Cars> carsList = gson.fromJson(carListAsString, type);
for (Cars cars : carsList){
Log.i("Car Data", cars.id+"-"+cars.name);
}
Run Code Online (Sandbox Code Playgroud)
希望这会节省时间,我救了它.
完成
Dhr*_*val 13
如果你使用通用数组列表与类而不是像 特定类型
EX:
private ArrayList<Model> aListModel = new ArrayList<Model>();
Run Code Online (Sandbox Code Playgroud)
这里,Model = Class
接收意图如下:
aListModel = (ArrayList<Model>) getIntent().getSerializableExtra(KEY);
Run Code Online (Sandbox Code Playgroud)
必须记住:
这里Model-class必须实现如下: ModelClass实现Serializable
假设您需要将以下类的数组列表从当前活动传递到下一个活动 // 数组列表中对象的类 // 记得从 Serializable 接口实现类 // Serializable 意味着它将对象转换为字节流并有助于转移那个对象
public class Question implements Serializable {
...
...
...
}
Run Code Online (Sandbox Code Playgroud)
在您当前的活动中,您可能有一个 ArrayList,如下所示
ArrayList<Question> qsList = new ArrayList<>();
qsList.add(new Question(1));
qsList.add(new Question(2));
qsList.add(new Question(3));
// intialize Bundle instance
Bundle b = new Bundle();
// putting questions list into the bundle .. as key value pair.
// so you can retrieve the arrayList with this key
b.putSerializable("questions", (Serializable) qsList);
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
i.putExtras(b);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)
为了在下一个活动中获取数组列表
//get the bundle
Bundle b = getIntent().getExtras();
//getting the arraylist from the key
ArrayList<Question> q = (ArrayList<Question>) b.getSerializable("questions");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
108406 次 |
最近记录: |