How do I click on the listview and open an activyity?

Asked

Viewed 66 times

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.

2 answers

2

Use this method:

 list.setOnItemClickListener(new AdapterView.onItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
      Intent appInfo = new Intent(YourActivity.this, ApkInfoActivity.class);
       startActivity(appInfo);
   } 
});
  • Thank you very much for your help. It worked perfectly. I would like one more adjustment if you can let me know, I can receive the position id, but I needed the name and email ex: Intent appInfo = new Intent(Main2activity.this, Mainactivity.class); startActivity(appInfo); Toast.makeText(Main2activity.this, "App "+name+email, Toast.LENGTH_LONG). show(); Anyway thanks a lot

0

A good place to put your click code would be on the Listview Adapter. In your Adapter (Customlistview in your case) put a code similar to this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = LayoutInflater.from(context).inflate(R.layout.seu_layout, null);
  view.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
      this.minhaLista.get(position); // Aqui você vai obter o item selecionado e com ele as informações que você quiser
      Intent intent = new Intent(context, NewsActivity.class);
      intent.putExtra("position", "qualquer coisa que você queira passar");
      Toast toast = Toast.makeText(this, "A mensagem que você quer", 500);
      toast.show();
      context.startActivity(intent);
     }
     return view;
 }

Something similar to the one above. Anyway take a look at this: https://blog.alura.com.br/personalizando-uma-listview-no-android/

And also consider switching to something more current if possible, a Recycleview for example: http://mobimais.com.br/blog/recyclerview-android-tutorial-facil/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.