Java Android-检查数组是否具有此值,获取位置并将intent put EXTRACT发送到下一个活动片段
关于这个问题,有人问了 基本上,我的FragmentActivity中有3个数组:Java Android-检查数组是否具有此值,获取位置并将intent put EXTRACT发送到下一个活动片段,java,android,arrays,android-fragments,Java,Android,Arrays,Android Fragments,关于这个问题,有人问了 基本上,我的FragmentActivity中有3个数组: String[] productName = {"Bread", "Butter", "Cadburry", "Tin Spinach", "Bulla Ice Cream", "Kit Kat Jam", "Chili Sauce", "Jasmine Rice", "Nutela", "Logan Tin" }; String[] expiryDate = {"27/September/20
String[] productName = {"Bread", "Butter", "Cadburry", "Tin Spinach", "Bulla Ice Cream", "Kit Kat Jam", "Chili Sauce", "Jasmine Rice", "Nutela", "Logan Tin" };
String[] expiryDate = {"27/September/2017", "30/September/2017", "30/October/2017", "28/September/2018", "2/January/2018", "29/December/2017", "27/September/2017", "28/September/2017", "13/February/2018", "20/December/2017"};
int[] images = {R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7, R.drawable.i8, R.drawable.i9, R.drawable.i10};
我想获得“expiryDate[]”中包含value=“27/Septer/2017”的所有职位
在获取元素在expiryDate[]中的位置后,我想使用从expiryDate[]为productName[]和images[]获取的位置,以基于expiryDate[]中的相同位置获取值
最后,我想将从productName[]、images[]和expiryDate[]获得的值转移到另一个FragmentActivity,并将它们放入新的数组中
有人能告诉我如何编写这个场景吗?我真的很感激。试试这个….
int position=-1;
position= Arrays.asList(array).indexOf("27/September/2017");
String name , image;
if(position>=0){
name = productName [position];
image = images [position];
}
> for(int i=0;i<expiryDate.lenght;i++){
> if(expiryDate[i].equals("27/September/2017"))
> {
> // do opeartion here
// add all values in new arrays
// send all new arrays to the next activity.
> }
> }
编辑:-请查看此…
int position=-1;
position= Arrays.asList(array).indexOf("27/September/2017");
String name , image;
if(position>=0){
name = productName [position];
image = images [position];
}
> for(int i=0;i<expiryDate.lenght;i++){
> if(expiryDate[i].equals("27/September/2017"))
> {
> // do opeartion here
// add all values in new arrays
// send all new arrays to the next activity.
> }
> }
>for(int i=0;i if(expiryDate[i].等于(“2017年9月27日”))
> {
>//在这里做作业
//在新数组中添加所有值
//将所有新阵列发送到下一个活动。
> }
> }
或
ArrayList<Int> positions = new ArrayList<>();
> for(int i=0;i<expiryDate.lenght;i++){
> if(expiryDate[i].equals("27/September/2017"))
> {
> positions.add(i);
// send all your existing array list with this positions array list to other activity
> }
> }
ArrayList positions=new ArrayList();
>对于(int i=0;i if(expiryDate[i].等于(“2017年9月27日”))
> {
>增加(i);
//将所有现有阵列列表和此位置阵列列表一起发送到其他活动
> }
> }
希望它能帮助……试试这个……
int position=-1;
position= Arrays.asList(array).indexOf("27/September/2017");
String name , image;
if(position>=0){
name = productName [position];
image = images [position];
}
> for(int i=0;i<expiryDate.lenght;i++){
> if(expiryDate[i].equals("27/September/2017"))
> {
> // do opeartion here
// add all values in new arrays
// send all new arrays to the next activity.
> }
> }
编辑:-请查看此…
int position=-1;
position= Arrays.asList(array).indexOf("27/September/2017");
String name , image;
if(position>=0){
name = productName [position];
image = images [position];
}
> for(int i=0;i<expiryDate.lenght;i++){
> if(expiryDate[i].equals("27/September/2017"))
> {
> // do opeartion here
// add all values in new arrays
// send all new arrays to the next activity.
> }
> }
>for(int i=0;i if(expiryDate[i].等于(“2017年9月27日”))
> {
>//在这里做作业
//在新数组中添加所有值
//将所有新阵列发送到下一个活动。
> }
> }
或
ArrayList<Int> positions = new ArrayList<>();
> for(int i=0;i<expiryDate.lenght;i++){
> if(expiryDate[i].equals("27/September/2017"))
> {
> positions.add(i);
// send all your existing array list with this positions array list to other activity
> }
> }
ArrayList positions=new ArrayList();
>对于(int i=0;i if(expiryDate[i].等于(“2017年9月27日”))
> {
>增加(i);
//将所有现有阵列列表和此位置阵列列表一起发送到其他活动
> }
> }
希望它有助于……按照以下代码获取项目在数组中的位置
int position=-1;
position= Arrays.asList(array).indexOf("27/September/2017");
String name , image;
if(position>=0){
name = productName [position];
image = images [position];
}
> for(int i=0;i<expiryDate.lenght;i++){
> if(expiryDate[i].equals("27/September/2017"))
> {
> // do opeartion here
// add all values in new arrays
// send all new arrays to the next activity.
> }
> }
String[] expiryDate = {"27/September/2017", "30/September/2017", "30/October/2017", "28/September/2018", "2/January/2018", "29/December/2017", "27/September/2017", "28/September/2017", "13/February/2018", "20/December/2017"};
private List<Integer> postition = new ArrayList<>();
for (int i = 0; i < expiryDate.length; i++) {
if (expiryDate[i].equals("27/September/2017")) {
postition.add(i);
}
}
Log.i("array positions", postition.toString());
private List<String> products = new ArrayList<>();
for (int i = 0; i < postition.size(); i++) {
products.add(productName[postition.get(i)]);
}
Log.e("products", products.toString());
String[]到期日={“2017年9月27日”、“2017年9月30日”、“2017年10月30日”、“2018年9月28日”、“2018年1月2日”、“2017年12月29日”、“2017年9月27日”、“2017年9月28日”、“2018年2月13日”、“2017年12月20日”};
私有列表位置=新建ArrayList();
对于(int i=0;i
使用此给定的expireDate数组,您可以在positions数组中获取值0和6。现在,要从productName和图像中获取值,请使用以下代码
String[] expiryDate = {"27/September/2017", "30/September/2017", "30/October/2017", "28/September/2018", "2/January/2018", "29/December/2017", "27/September/2017", "28/September/2017", "13/February/2018", "20/December/2017"};
private List<Integer> postition = new ArrayList<>();
for (int i = 0; i < expiryDate.length; i++) {
if (expiryDate[i].equals("27/September/2017")) {
postition.add(i);
}
}
Log.i("array positions", postition.toString());
private List<String> products = new ArrayList<>();
for (int i = 0; i < postition.size(); i++) {
products.add(productName[postition.get(i)]);
}
Log.e("products", products.toString());
private List products=new ArrayList();
对于(int i=0;i
现在,您可以为现有的productName阵列获取面包和辣椒酱的值。对图像也可以这样做
希望这能有所帮助。要获取项目在数组中的位置,请按照以下代码进行操作
String[] expiryDate = {"27/September/2017", "30/September/2017", "30/October/2017", "28/September/2018", "2/January/2018", "29/December/2017", "27/September/2017", "28/September/2017", "13/February/2018", "20/December/2017"};
private List<Integer> postition = new ArrayList<>();
for (int i = 0; i < expiryDate.length; i++) {
if (expiryDate[i].equals("27/September/2017")) {
postition.add(i);
}
}
Log.i("array positions", postition.toString());
private List<String> products = new ArrayList<>();
for (int i = 0; i < postition.size(); i++) {
products.add(productName[postition.get(i)]);
}
Log.e("products", products.toString());
String[]到期日={“2017年9月27日”、“2017年9月30日”、“2017年10月30日”、“2018年9月28日”、“2018年1月2日”、“2017年12月29日”、“2017年9月27日”、“2017年9月28日”、“2018年2月13日”、“2017年12月20日”};
私有列表位置=新建ArrayList();
对于(int i=0;i
使用此给定的expireDate数组,您可以在positions数组中获取值0和6。现在,要从productName和图像中获取值,请使用以下代码
String[] expiryDate = {"27/September/2017", "30/September/2017", "30/October/2017", "28/September/2018", "2/January/2018", "29/December/2017", "27/September/2017", "28/September/2017", "13/February/2018", "20/December/2017"};
private List<Integer> postition = new ArrayList<>();
for (int i = 0; i < expiryDate.length; i++) {
if (expiryDate[i].equals("27/September/2017")) {
postition.add(i);
}
}
Log.i("array positions", postition.toString());
private List<String> products = new ArrayList<>();
for (int i = 0; i < postition.size(); i++) {
products.add(productName[postition.get(i)]);
}
Log.e("products", products.toString());
private List products=new ArrayList();
对于(int i=0;i
现在,您可以为现有的productName阵列获取面包和辣椒酱的值。对图像也可以这样做
希望这会有所帮助。如果这三个数组是相关的,您应该真正利用封装来维护数据之间的关系。这样更易于维护和读取。它应该允许您更改数据,而不必担心保持三个数组的同步(避免您遇到不可避免的索引越界问题) 这样做之后,过滤一个大列表就容易多了:
List<Product> myProducts = ...;
List<Product> filteredProducts = new ArrayList<>();
for (Product product : myProducts){
if (product.expiration.equals("27/September/2017")){
filteredProducts.add(product);
}
}
// by now, filteredProducts should contain all the items you wanted
如果这三个数组是相关的,您应该真正利用封装来维护数据之间的关系。这样更易于维护和读取。它应该允许您更改数据,而不必担心保持3个数组的同步(以避免您遇到不可避免的索引越界问题) 这样做之后,过滤一个大列表就容易多了:
List<Product> myProducts = ...;
List<Product> filteredProducts = new ArrayList<>();
for (Product product : myProducts){
if (product.expiration.equals("27/September/2017")){
filteredProducts.add(product);
}
}
// by now, filteredProducts should contain all the items you wanted
获取图像和产品名称:
ArrayList<String> expiredProductNames = new ArrayList<>();
ArrayList<Integer> expiredProductImages = new ArrayList<>();
String expiredDate = "27/September/2017";
for (int i = 0; i < expiryDate.length; i++) {
if (expiryDate[i].equals(expiredDate)) {
expiredProductNames.add(productName[i]);
expiredProductImages.add(images[i]);
}
}
Intent intent = new Intent(FragmentActivityA.this, FragmentActivityB.class);
intent.putStringArrayListExtra("productNames", expiredProductNames);
intent.putIntegerArrayListExtra("images", expiredProductImages);
ArrayList<String> productNames;
ArrayList<Integer> images;
Intent intent = getIntent();
if (null != intent) {
productNames = intent.getStringArrayListExtra("productNames");
images = intent.getIntegerArrayListExtra("images");
}
在第二个片段活动中从意图中检索值:
ArrayList<String> expiredProductNames = new ArrayList<>();
ArrayList<Integer> expiredProductImages = new ArrayList<>();
String expiredDate = "27/September/2017";
for (int i = 0; i < expiryDate.length; i++) {
if (expiryDate[i].equals(expiredDate)) {
expiredProductNames.add(productName[i]);
expiredProductImages.add(images[i]);
}
}
Intent intent = new Intent(FragmentActivityA.this, FragmentActivityB.class);
intent.putStringArrayListExtra("productNames", expiredProductNames);
intent.putIntegerArrayListExtra("images", expiredProductImages);
ArrayList<String> productNames;
ArrayList<Integer> images;
Intent intent = getIntent();
if (null != intent) {
productNames = intent.getStringArrayListExtra("productNames");
images = intent.getIntegerArrayListExtra("images");
}
ArrayList产品名称;
阵列图像;
Intent=getIntent();
if(null!=intent){
productNames=intent.GetStringArrayListXTRA(“productNames”);
images=intent.getIntegerArrayListExtra(“images”);
}
注意:
最好使用类来封装数据,这样就只有一个列表可以像其他建议一样迭代和传播。获取图像和产品名称:
ArrayList<String> expiredProductNames = new ArrayList<>();
ArrayList<Integer> expiredProductImages = new ArrayList<>();
String expiredDate = "27/September/2017";
for (int i = 0; i < expiryDate.length; i++) {
if (expiryDate[i].equals(expiredDate)) {
expiredProductNames.add(productName[i]);
expiredProductImages.add(images[i]);
}
}
Intent intent = new Intent(FragmentActivityA.this, FragmentActivityB.class);
intent.putStringArrayListExtra("productNames", expiredProductNames);
intent.putIntegerArrayListExtra("images", expiredProductImages);
ArrayList<String> productNames;
ArrayList<Integer> images;
Intent intent = getIntent();
if (null != intent) {
productNames = intent.getStringArrayListExtra("productNames");
images = intent.getIntegerArrayListExtra("images");
}
在第二个片段活动中从意图中检索值:
ArrayList<String> expiredProductNames = new ArrayList<>();
ArrayList<Integer> expiredProductImages = new ArrayList<>();
String expiredDate = "27/September/2017";
for (int i = 0; i < expiryDate.length; i++) {
if (expiryDate[i].equals(expiredDate)) {
expiredProductNames.add(productName[i]);
expiredProductImages.add(images[i]);
}
}
Intent intent = new Intent(FragmentActivityA.this, FragmentActivityB.class);
intent.putStringArrayListExtra("productNames", expiredProductNames);
intent.putIntegerArrayListExtra("images", expiredProductImages);
ArrayList<String> productNames;
ArrayList<Integer> images;
Intent intent = getIntent();
if (null != intent) {
productNames = intent.getStringArrayListExtra("productNames");
images = intent.getIntegerArrayListExtra("images");
}
ArrayList产品名称;
阵列图像;
Intent=getInte