-1

I have a ListView in the MainActivity which can be edited by the user (user can add new items to he list) I´m wondering if it´s possible to create a new Activity when a new Item is created so that when it´s clicked, it displays information about the item in another screen.

Regards

  • 1
    Possible duplicate of [How to start new activity on button](https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – Syed Ahmed Jamil Nov 24 '19 at 21:39

2 Answers2

1

You can create a "template" Activity with empty widgets displaying info about any object that fits the template. Simply, you set the text and content when you want (when user adds new object).

eg. I have this BrowserActivity where there is a ListView of Song objects. When user clicks on one the app gets the object from the list and puts it to the second "template" activity.

ListView list;

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

        list = findViewById(R.id.song_item_list_view);

        SongItemArrayAdapter adapter = new SongItemArrayAdapter(this, Generator.getSampleSongs());
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Song item = (Song) list.getItemAtPosition(position);
                Intent intent = new Intent(BrowserActivity.this, SongViewActivity.class);
                intent.putExtra("EXTRA_SONG_ID", item.getId());
                intent.putExtra("EXTRA_ROOT_ACTIVITY", SongViewActivity.Roots.browserActivity.toString());
                startActivity(intent);
            }
        });
    }

and the template SongViewActivity

String rootActivity;
    int songsID;
    TextView title;
    TextView text;
    TextView author;
    Song displayedSong;

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

        title = findViewById(R.id.title_song_tv);
        text = findViewById(R.id.tv_scrolling_song_text);
        author = findViewById(R.id.author_song_tv);
        songsID = getIntent().getExtras().getInt("EXTRA_SONG_ID");
        rootActivity = getIntent().getExtras().getString("EXTRA_ROOT_ACTIVITY");

        for(Song song : Generator.getSongs()){
            if(song.getId() == songsID){
                author.setText(song.getAuthor());
                title.setText(song.getTittle());
                text.setText(song.getText());
                displayedSong = song;
            }
        }
Lazy
  • 257
  • 2
  • 10
0

It is possible. To achieve that you need to implement an OnItemClickListener and set it to your listView. In your listener you should raise an intent and call the new activity with:

Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);

it shows you how: https://developer.android.com/training/basics/firstapp/starting-activity

Rander Gabriel
  • 637
  • 4
  • 14