简体   繁体   English

如何从android中的One Arraylist获取Separate Data Item值

[英]How to get Separate Data Item value from One Arraylist in android

Hello Friends i have one custom arraylist which include items like : [t_id(PK) cat_id(FK), cat_name,total] . 你好朋友我有一个自定义arraylist,其中包括如下项目: [t_id(PK) cat_id(FK), cat_name,total] .

Suppose i enter five value on it like ({1,1,c1,100},{2,1,c1,200},{3,1,c1,300},{4,1,c2,400},{5,1,c2,500}) 假设我在其上输入五个值,如({1,1,c1,100},{2,1,c1,200},{3,1,c1,300},{4,1,c2,400},{5,1,c2,500})

NOw i want Total value of Category "c1" and "c2" 不,我想要类别"c1""c2"总价值

Means in output it should be like 输出中的意思应该是

"c1=600" and "c2=900".

So any idea how can i solve it 所以任何想法我怎么能解决它

I dont know how the bean was, here i tried something that may help you, 我不知道这是怎么回事,在这里我尝试了一些可以帮到你的东西,

        Map<String, Integer> map = new HashMap<String, Integer>();
        for(Custom custom : customList)
        {
            if(map.containsKey(custom.getCat_name()))
            {
                Integer i = map.get(custom.getCat_name());
                map.put(custom.getCat_name(), i+custom.getTotal());
            }
            else
            {
                map.put(custom.getCat_name(), custom.getTotal());
            }
        }

        System.out.println("Map : "+map);

Output : Map : {c1=600, c2=900} 输出: Map : {c1=600, c2=900}

It's guess but what I understood is like this 这是猜测,但我理解的是这样的

public class Custom
{
int t_id(PK);
int cat_id(FK);
String cat_name;
int total;

public Custom(int t_id(PK),int cat_id(FK),String cat_name,int total)
{
this.t_id(PK)=t_id; 
this.cat_id(FK)=cat_id(FK);
this.cat_name=cat_name;
this.total=total;
}
}

ArrayList<Custom> list=new ArrayList<Custom>;

Now if you want to get total no for c1 现在,如果你想获得c1总数

Do it like this. 像这样做。

int totalC1=0;
for(Custom c:list)
{
if(c.cat_name.equals("c1"))
{
totalC1+=c.total;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM