0
I need to click on the view list and open a new activyte with the person’s name.
Code:
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main2Activity extends AppCompatActivity {
String urladdress="https://wwwmeusite";
String[] name;
String[] email;
String[] imagepath;
ListView listView;
BufferedInputStream is;
String line=null;
String result=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView=(ListView)findViewById(R.id.lview);
StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
collectData();
CustomListView customListView=new CustomListView(this,name,email,imagepath);
listView.setAdapter(customListView);
}
private void collectData()
{
//Connection
try{
URL url=new URL(urladdress);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
is=new BufferedInputStream(con.getInputStream());
}
catch (Exception ex)
{
ex.printStackTrace();
}
//content
try{
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
while ((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}
catch (Exception ex)
{
ex.printStackTrace();
}
//JSON
try{
JSONArray ja=new JSONArray(result);
JSONObject jo=null;
name=new String[ja.length()];
email=new String[ja.length()];
imagepath=new String[ja.length()];
for(int i=0;i<=ja.length();i++){
jo=ja.getJSONObject(i);
name[i]=jo.getString("nome");
email[i]=jo.getString("email");
imagepath[i]=jo.getString("photo");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
I needed to know where I put the code to open a new Activity.
– Alexadre