I'm working on an app in which I need to retrieve data from two different json and I have to populate a listview in android. In the first Json I have an array of object and I have the id object and an url that need to retrieve data for that object.
How can I do that? I can retrieve the object and populate listview with this object but I don't know how to take the data for the single object and show it in the same row.
The list view should be like this:
This is Json1:
[
{
"id": "S1",
"url": "MyUrl1"
},
{
"id": "S2",
"url": "MyUrl2"
},
{
"id": "S3",
"url": "MyUrl3"
}
]
It's different for every object. This is Json2:
{
"id": "S1",
"temp": "20.03",
"time": "28 June 2018, 9:12:13"
}
So, I can retrieve id and show all id object in listView, but I cannot retrieve for every object the "temp" and "time" values.
This is my code:
Adapter
public class SensorAdapter extends BaseAdapter{
ArrayList<String> id= new ArrayList<String>();
ArrayList<String> labelno_array = new ArrayList<String>();
Context context;
LayoutInflater inflater;
public SensorAdapter2(Context c, ArrayList<String> label_array)
{
context = c;
id= label_array;
// temp=temp_array;
// time=time_array;
//labelno_array = no_array;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
Holder holder;
// TODO Auto-generated method stub
if (v == null) {
v = inflater.inflate(R.layout.columns_sensor, null);
holder = new Holder();
holder.tv_labelname = (TextView)v.findViewById(R.id.idsensor);
v.setTag(holder);
} else {
holder = (Holder) v.getTag();
}
holder.tv_labelname.setText(id.get(position));
return v;
}
public class Holder
{
TextView tv_labelname,tv_labelno, tv_temp, tv_time;
}
And this is my code to retrieve the number of sensor:
Code
class RetrieveFeedTask2 extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(Void... urls) {
String API_URL = "MyUrl";
try {
URL url = new URL(API_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
// progressBar.setVisibility(View.GONE);
Log.i("INFO", response);
try
{
JSONArray array=new JSONArray(response);
for(int i = 0; i < array.length(); i++)
{
JSONObject obj = array.getJSONObject(i);
String mysenso=obj.getString("id");
String myurl=obj.getString("url");
//partire nuova chiamata con l'url.
label_no.add(myurl);
label_name.add(mysenso);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
SensorAdapter2 adapter = new SensorAdapter2(Principale.this, label_name);
listView.setAdapter(adapter);
}
}
So, how can I connect at the object url and update data for that object in the same row?