'How to set two arraylist with different size inside one adapter?
I have two arraylist which i use for storing value from JSON which have different path database. I want populate it with one custom adapter with this such way
- First arraylist contain 2 name_stage and second arraylist contain 4 revenue and name_stage.
- For second arraylist, i need loop for sum revenue and count data if name_stage is same.
- One row will contain name_stage, result of sum revenue, and count data
- First arraylist size will used for inflate row
This my JSON structure with dummy data
First JSON
{"result":[
{"id":1,
"name_stage":"New"},
{"id":2,
"name_stage":"Closing"}
]
}
Second JSON
{"result":[
{"id":1,
"name_stage":"New",
"revenue":3000},
{"id":2,
"name_stage":"Closing",
"revenue":10000},
{"id":3,
"name_stage":"New",
"revenue":2000},
{"id":4,
"name_stage":"Closing",
"revenue":5000}
]
}
I found similar case in this but i cant still figure this in my case
I try for parsing JSON and looping that (for second arraylist)
detail = new HashMap<>();
detail = new Gson().fromJson(_response, new TypeToken<HashMap<String, Object>>(){}.getType());
str= (new Gson()).toJson(detail.get("result"), new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());
listmap_detail = new Gson().fromJson(str, new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());
for (int i=0;i<listmap_detail.size();i++){
if(listmap_detail.get(a).get("stage_id").toString().equals("New")){
count_new+=1;
new_revenue+=Double.parseDouble(listmap_detail.get(a).get("revenue").toString());
}else if(listmap_detail.get(a).get("name_stage").toString().equals("Closing")){
count_closing+=1; closing_revenue+=Double.parseDouble(listmap_detail.get(a).get("revenue").toString());
}
}
after that i try for passing it with intent and use that inside adapter like this
Intent i = new Intent();
i.setClass(this, NextActivity.class);
i.putExtra("new_revenue", new_revenue);
i.putExtra("closing_revenue", closing_revenue);
i.putExtra("new_count", count_new);
i.putExtra("closing_count", count_closing);
startActivity(i);
Adapter in NextActivity
@Override
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.c_opportunity_stage, null);
}
final TextView t_stagenumber = (TextView) _view.findViewById(R.id.c_stagecount);
final TextView t_stage = (TextView) _view.findViewById(R.id.c_stage);
final TextView t_countstage = (TextView) _view.findViewById(R.id.c_countstage);
final TextView t_revenue = (TextView) _view.findViewById(R.id.c_totalrevenue);
t_stagenumber.setText(String.valueOf((long)(_position + 1)).concat("."));
t_stage.setText(_data.get(_position).get("name_stage").toString());
if (_data.get(_position).get("name_stage").toString().equals("New")){
count = Double.valueOf(getIntent().getStringExtra("new_count"));
t_countstage.setText(String.valueOf(count));
str2 = getIntent().getStringExtra("new_revenue");
dataresult = Double.parseDouble(str2);
tampung = new Double(str2);
bagi = tampung.intValue();
a = 0;
while (bagi!=0){
bagi/=10;
++a;
}
if (a>=5&&a<7){
dataresult/=Double.valueOf(1000);
value = String.valueOf(dataresult);
t_revenue.setText(value+" "+"K");
}
if (a>=7){
dataresult/=Double.valueOf(1000000);
value = String.valueOf(dataresult);
t_revenue.setText(value+" "+"M");
}
if (a==0){
t_revenue.setText("0");
}
}else if (_data.get(_position).get("name_stage").toString().equals("Closing")){
count = Double.valueOf(getIntent().getStringExtra("closing_count"));
t_countstage.setText(String.valueOf(count));
str2 = getIntent().getStringExtra("closing_revenue");
dataresult = Double.parseDouble(str2);
tampung = new Double(str2);
bagi = tampung.intValue();
a = 0;
while (bagi!=0){
bagi/=10;
++a;
}
if (a>=5&&a<7){
dataresult/=Double.valueOf(1000);
value = String.valueOf(dataresult);
t_revenue.setText(value+" "+"K");
}
if (a>=7){
dataresult/=Double.valueOf(1000000);
value = String.valueOf(dataresult);
t_revenue.setText(value+" "+"M");
}
if (a==0){
t_revenue.setText("0");
}
}
}
This way already works but this bad way and will make my adapter error if intent value equals null. Can some guide me for this problem?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
