Object of listview for each Manactivity

Asked

Viewed 36 times

0

I would like to call a Manactivity for each object of the listview, without passing anything to the new one because I will create the content of the separate Manactivitys. However as this the code lowers, any item I click, calls the same Manactivity. I want to call Manactivity1, 2 and Tc for each object. package com.example.listview;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telecom.Call;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import javax.crypto.AEADBadTagException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lista = (ListView) findViewById(R.id.lvEscolas);
        final ArrayList <Escola> escolas = adicionarEscolas();
        ArrayAdapter adapter = new EscolaAdapter(this, escolas);
        lista.setAdapter(adapter);    

        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {    
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);

                //intent.putExtra("nome", escolas.get(i).getNome());

                startActivity(intent);

1 answer

-1

For your problem here is the solution.

   //modelo 1
    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Escola escola = (Escola) adapter.getItem(i);

            Intent intent;

            switch (escola.getId()) {
                case 1:
                    intent = new Intent(MainActivity.this, MinhaAtividadeA.class);
                    break;
                case 2:
                    intent = new Intent(MainActivity.this, MinhaAtividadeB.class);
                    break;
                default:
                    intent = new Intent(MainActivity.this, MinhaAtividadeDefault.class);
                    break;
            }

            startActivityForResult(intent, 1);
        }
    });

You can use the switch to pick up the unique identifier if the data is fixed, or you can use other forms of validation to start the screen individually.

Or you can be bolder and by an Activity within the school object itself

//modelo 2
    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Escola escola = (Escola) adapter.getItem(i);

            Intent intent = new Intent(MainActivity2.this, escola.getClasse());

            startActivityForResult(intent, 1);
        }
    });

In school class

public class Escola {
    private Class classe;

public Class getClasse() {
    return classe;
}

public void setClasse(Class classe) {
    this.classe = classe;
}

Browser other questions tagged

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