0

I'm having trouble launching an instance of a class with an intent.

My app has an initial screen with three textviews 'Cakes, Drinks, and Biscuits'. An onClick listener is set on these textviews, such that when the user clicks on 'Cakes' it goes to a list of cakes (Chocolate, Lemon, Carrot), and the user clicks on 'Drinks' it goes to a list of drinks (Tea, Coffee, Coke), and the same for Biscuits. Currently, I have an intent that launches a Cakes class, and within that class there is an arrayList that has strings (Chocolate, Lemon, Carrot etc.), when the user clicks on Drinks it launches a separate class called Drinks and does the same, ditto with Biscuits. However, I gather it is better coding practice to have simply one class for these three, and have separate instances of that class (with different arrayLists to separately list the cake, drinks and biscuits).

I assume I should make an 'items' class (i.e. the arrayList with Chocolate, Lemon, Carrot, and all the drinks, and all the biscuits) but don't know how to launch a specific instance of the 'items' class with my intent written in the mainActivity. Any help appreciated!

The mainActivity.java where the initial three items are listed:

package com.example.android.recipe;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    TextView history = (TextView) findViewById(R.id.cakes);
    history.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent cakesList = new Intent(MainActivity.this, cakeActivity.class);
            startActivity(cakesList);
        }
    });

    TextView examination = (TextView) findViewById(R.id.drinks);
    examination.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent drinksIntent = new Intent(MainActivity.this, drinksActivity.class);
            startActivity(drinksIntent);
        }
    });

    TextView other = (TextView) findViewById(R.id.biscuits);
    other.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent biscuitsIntent = new Intent(MainActivity.this, BiscuitsActivity.class);
            startActivity(biscuitsIntent);
        }
    });

    }
}

This is the 'CakeActivity.java' which gets launched when you click on 'Cakes', I would like to turn this into a generic class which gets launched when you click on Cakes, Coffee, or Biscuits but it comes up with the relevant ArrayList. NB I have a custom ArrayAdapter called 'Record' because, when I further develop this app, clicking on Chocolate, Lemon, etc. will have a list of separate ingredients required to make that Cake (these will also in the future not be separate classes).

package com.example.android.recipe;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class cakeActivity extends AppCompatActivity {

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

        final ArrayList<Record> records = new ArrayList<Record>();

        records.add(new Record("Chocolate", ChocolateCake.class));
        records.add(new Record("Lemon", LemonCake.class));
        records.add(new Record("Ginger", GingerCake.class));

        RecordAdapter adapter =
                new RecordAdapter(this, records, R.color.colorTan);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Record record = records.get(position);
                Intent intent = new Intent(getApplicationContext(), record.getListedItemId());
                startActivity(intent);
            }
            });



    }
}

NB this is exemplary code, and not my actual app - the reason I mention this is because I am going to have a really long list that, in the way in which my code is currently written, would require >100 classes if I continued in the way I am above.

  • Use the Intent to pass data (e.g. which type of List) to the next Activity, see [this post](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application). – Bö macht Blau Sep 26 '17 at 16:34

1 Answers1

0

Yyou can use intent extra value:

The easiest way to do this would be to pass the type to the activity in the intent.

Intent intent = new Intent(getBaseContext(), AllInOneActivity.class);
intent.putExtra("type", "cake");
startActivity(intent);

Access that intent on next activity

String type = getIntent().getStringExtra("type");

Load your data according to type:

final ArrayList<Record> records = new ArrayList<Record>();
if(type.equals("cake"))
{
        records.add(new Record("Chocolate", ChocolateCake.class));
        records.add(new Record("Lemon", LemonCake.class));
        records.add(new Record("Ginger", GingerCake.class));
}
else if(type.equals("Drinks"))
{
records.add(new Record("fruty", whatever.class));
}

Try this and let me know. if problem persist.

Harminder Singh
  • 316
  • 1
  • 11