SkyeBoniwell
SkyeBoniwell

Reputation: 7092

Dynamically add data from one activity to another

I have a little app that I'm working on that displays a list of monsters. You can click on each monster to see more info about that monster.

The list is in my xml layout file in a TableLayout.

But looking at the code, it's easy to see how things can get out of hand the more monsters I add.

I was wondering, if there is a better way of determing which monster was clicked and how to get the needed info passed from the list of monsters view to the monster info view.

Here is my code so far. It works fine, but I know it's not really good because I'd have to add a new case statement for each new monster I add to the list.

public void AddNewView(View view) {
        //get selected button view
        switch(view.getId())
        {
            //get textview & imageview monsters from xml 
            //get monster picture and monster info from xml
            case R.id.showMonsterButton1:
                iv  = (ImageView) findViewById(R.id.monster1_icon);
                tv = (TextView) findViewById(R.id.monster1_info);
                ShowMonsterInfoView(tv, iv);
                break;

            case R.id.showMonsterButton2:
                iv  = (ImageView) findViewById(R.id.monster2_icon);
                tv = (TextView) findViewById(R.id.monster2_info);
                ShowMonsterInfoView(tv, iv);
                break;

            case R.id.showMonsterButton3:
                iv  = (ImageView) findViewById(R.id.monster3_icon);
                tv = (TextView) findViewById(R.id.monster3_info);
                ShowMonsterInfoView(tv, iv);
                break;

            case R.id.showMonsterButton4:
                iv  = (ImageView) findViewById(R.id.monster4_icon);
                tv = (TextView) findViewById(R.id.monster4_info);
                ShowMonsterInfoView(tv, iv);
                break;
        }
    }

    public void ShowMonsterInfoView(TextView tv, ImageView iv) {
        Intent intent = new Intent(this, DisplayMonsterInfo.class);
        String text_tag = tv.getTag().toString();
        String image_tag = iv.getTag().toString();
        Bundle extras = new Bundle();
        extras.putString("name", text_tag);
        extras.putString("avatar", image_tag);
        intent.putExtras(extras);
        startActivity(intent);

    }

Upvotes: 0

Views: 141

Answers (3)

Prajjwal Srivastav
Prajjwal Srivastav

Reputation: 392

Save your list of monsters in a List object and pass it in an ArrayAdapter object which will be used to populate the ListView. In the List object you can dynamically add more monsters on the go using obj.add(monster_name). In the setOnItemSelected method save the the selected monster in a static String object, which can be accessed in any Activity using MonsterListActivity.monster_name. Use it to pull data from file/database.

Upvotes: 1

Arpan Sharma
Arpan Sharma

Reputation: 2162

What you can do is simply save data to DB and assign a unique id to every monster.On the click you pass the id to the next activity and fetch the data about the monster their.

On the other hand if you don't want to save the data, pass the data with the click intent to other activity(i.e if you have the data in the first activity).

As you have to add views, I recommended you use recycler view where you can simply add the items in the list you are providing and call notifyDataSetChanged().Using table layout for this might not be a good idea.

Upvotes: 1

Divers
Divers

Reputation: 9569

As per how to get which monster was clicked. In case if you are using RecyclerView and in case if ListView just setOnItemClickListener. By position of clicked item you can get object from list with monsters objects and then get id from there.

Put data about monster in data base and pass id of monster to another activity where you will fetch data from DB.

Upvotes: 1

Related Questions