1

I'm trying to pass information from my second activity to my first activity. In my second Activity contains a list view of images of provinces in Canada. Each item in the list contains 3 properties (Province Name, Capital of the Province, Image id for the actual flag of the province). When the user selects an item from the listView it should direct them to the first Activity and pass the intent information from the second Activity to the first Activity. The information that will be passed are the name of the province and the capital of the province. I feel like I'm doing it correctly and my app currently has no issue in terms of bugs or crashes but I can't seem to figure out why the information is not being passed from activity 2 to activity 1. It just passes nothing. Thanks to those all in advance that help me out!

Main Activity.Java

    TextView provinceTxt, capitalTxt;
    String provinceString, capitalString;
    ArrayList<Province> provinces;

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


        provinceTxt = findViewById(R.id.txtProvince);
        capitalTxt = findViewById(R.id.txtCapital);

        display();


    }

    public void selectProvince(View view) {
        showAlertDialog();

    }

    public void showAlertDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Would You Like to Open the Second Activity?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MainActivity.this, Second_Activity.class);
                        startActivity(intent);
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    public void display(){
        Intent intent = getIntent();

        provinceString = intent.getStringExtra("name");
        provinceTxt.setText(provinceString);

        capitalString =  intent.getStringExtra("capital");
        capitalTxt.setText(capitalString);



    }
}

Second Activity.Java

    List<Province> provinces;

    ListView listView;

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

        provinces = ProvincesData.getList();
        listView = findViewById(R.id.listView);

        CustomAdapter adapter = new CustomAdapter(Second_Activity.this, R.layout.list_view_row, provinces);

        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

               Province province = (Province) listView.getItemAtPosition(position);

                Intent intent = new Intent();

                intent.putExtra("name", province.getName());
                intent.putExtra("capital", province.getCapital());

                finish();


            }
        });


    }

}

Province.Java

public class Province   {
    String name;
    String capital;

    public Province(String name, String capital, int armID) {
        this.name = name;
        this.capital = capital;
        this.armID = armID;
    }

    int armID;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCapital() {
        return capital;
    }

    public void setCapital(String capital) {
        this.capital = capital;
    }

    public int getArmID() {
        return armID;
    }

    public void setArmID(int armID) {
        this.armID = armID;
    }


}

ProvincesData.Java


    public static List<Province> getList() {
        List<Province> provinces = new ArrayList<Province>();
        Province province1 = new Province("Alberta","Edmonton", R.drawable.alberta);
        provinces.add(province1);
        Province province2 = new Province("British Columbia","Victoria", R.drawable.british_columbia);
        provinces.add(province2);
        Province province3 = new Province("Manitoba","Winnipeg", R.drawable.manitoba);
        provinces.add(province3);
        Province province4 = new Province("New Brunswick","Fredericton", R.drawable.new_brunswick);
        provinces.add(province4);
        Province province5 = new Province("Newfoundland and Labrador","St. John's", R.drawable.newfoundland_and_labrador);
        provinces.add(province5);
        Province province6 = new Province("Nova Scotia","Halifax", R.drawable.nova_scotia);
        provinces.add(province6);
        Province province7 = new Province("Ontario","Toronto", R.drawable.ontario);
        provinces.add(province7);
        Province province8 = new Province("Quebec","Quebec City", R.drawable.quebec);
        provinces.add(province8);
        Province province9 = new Province("Saskatchewan","Regina", R.drawable.saskatchewan);
        provinces.add(province9);
        Province province10 = new Province("Prince Edward ","Charlottetown", R.drawable.prince_edward_island);
        provinces.add(province10);

        return provinces;
    }

}

CustomAdapter.Java

public class CustomAdapter extends ArrayAdapter {
    static class ViewHolder{
        TextView provinceTv;
        TextView cityTv;
        ImageView armID;
    }

    List<Province> provinces;
    public CustomAdapter(@NonNull Context context, int resource, @NonNull List objects) {
        super(context, resource, objects);
        provinces = objects;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        View view  = convertView;
        if(view == null){
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list_view_row , null);
            holder = new ViewHolder();
            holder.provinceTv = view.findViewById(R.id.txtProvinceRow);
            holder.cityTv = view.findViewById(R.id.txtCapitalRow);
            holder.armID = view.findViewById(R.id.imageView);
            view.setTag(holder);
        }

        holder = (ViewHolder)view.getTag();

        holder.provinceTv.setText(provinces.get(position).getName());
        holder.cityTv.setText(provinces.get(position).getCapital());
        holder.armID.setBackgroundResource(provinces.get(position).getArmID());

        return view;
    }

}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:text="Selected Province Info"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginHorizontal="25dp"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/txtProvinceTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Province Name"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtProvince"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="text"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginHorizontal="25dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/txtCapitalTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Capital City"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/txtCapital"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="text"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    </LinearLayout>

    <Button
        android:id="@+id/btnSelect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginHorizontal="30dp"
        android:text="Select Province"
        android:onClick="selectProvince"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textStyle="bold"/>

</LinearLayout>

Activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Second_Activity">


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

list_view_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginHorizontal="10dp"
        android:layout_weight="1"
        tools:srcCompat="@tools:sample/avatars" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:layout_weight="2"
        android:layout_marginHorizontal="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtProvinceRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textStyle="bold"
            android:text="" />

        <TextView
            android:id="@+id/txtCapitalRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:text="" />
    </LinearLayout>
</LinearLayout>
Bob
  • 61
  • 6
  • You really should be using `startActivityForResult()`, see here: https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android – Daniel Nugent Oct 24 '19 at 23:29

2 Answers2

1

You forget to start the intent

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

               Province province = (Province) listView.getItemAtPosition(position);

                Intent intent = new Intent(this.Second_Activity.class, MainActivity);


                intent.putExtra("name", province.getName());
                intent.putExtra("capital", province.getCapital());

                startActivity(intent);

                finish();



            }
        });

receive the intent

 public void display(){
        Bundle extras = intent.getExtras();
        if(extras != null){

        String provinceString = extras.getString("name");
        provinceTxt.setText(provinceString);

        String  capitalString =  extras.getString("capital");
        capitalTxt.setText(capitalString);
      }


    } 

for more details refer this

Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33
0

you can use Activity.startActivityForResult(Intent intent, int requestCode)to start Second Activity and use Activity.onActivityResult(int requestCode, int resultCode, Intent data) to receive data that from Second Activityin MainActivity