0

I have a small dilemma with the app I am making. Basically, the app is for a game, to display information about each creature in the game. There are at least 30 creatures in the game, so I made a class and layout file for each creature, so I could display different information for each one. The question I am asking, is, is there a way for me to have like one static class file and when I clicked a button to go to one creatures' page, instead of creating a whole class and layout file for that one creature, it would just change the all the strings in the one static class to the information I need? I am very new to Java programming so I only know a few things. I was just wanting to make the code a little cleaner. Thanks for the help!

Here is one of the Creature's class files (there are over 20 of these doing absolutely nothing). Here is an example of Bat.java:

public class Bat extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String userTheme = prefs.getString("theme", "main");

        if (userTheme.equals("main"))
            setTheme(R.style.MainTheme);
        else if (userTheme.equals("light"))
            setTheme(R.style.HoloLight);
        else if (userTheme.equals("lightdark"))
            setTheme(R.style.HoloLightDark);
        else if (userTheme.equals("dark"))
            setTheme(R.style.HoloTheme);
        setContentView(R.layout.bat);

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        inflater.inflate(R.menu.options, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.statistics:
                Intent intent0 = new Intent(Bat.this, Statistics.class);
                startActivity(intent0);
                return true;
            case R.id.funfacts:
                Intent intent1 = new Intent(Bat.this, FunFacts.class);
                startActivity(intent1);
                return true;
            case R.id.home:
                Intent intent2 = new Intent(Bat.this, Home.class);
                startActivity(intent2);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

The real info for the creatures goes on my xml files (which I also have over 20 of) Here is an example of Bat.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="144dp"
        android:src="@drawable/batmc" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Overview"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bats are the second smallest mob in the game. They will spawn only in large caves, or with a spawn egg. They may also spawn in a house, considering its dark, and big enough."
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Behavior"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bats sleep during the day, and become active at night. While idle, a bat will hang upside down until a player approaches, which it will then fly away. They cannot hang on non-solid or transparent blocks. If you place a bat in a minecart, it will move the minecart on its own."
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Drops"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="None"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:visibility="invisible" />

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <View
    android:layout_height="2dip"
    android:background="#FF909090" />

    <TextView
        android:layout_column="1"
        android:text="Spawn - Light level 10 or less"
        android:padding="3dip"
        android:gravity="left"
        android:textSize="18dip" />
<View
    android:layout_height="2dip"
    android:background="#FF909090" />

    <TextView
        android:layout_column="1"
        android:text="Health - 6"
        android:padding="3dip"
        android:textSize="18dip" />

<View
    android:layout_height="2dip"
    android:background="#FF909090" />



    <TextView
        android:layout_column="1"
        android:text="First Appearance - 12w28a"
        android:padding="3dip"
        android:textSize="18dip" />

<View
    android:layout_height="2dip"
    android:background="#FF909090" />


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:text="Network ID - 65"
        android:padding="3dip"
        android:textSize="18dip" />

<View
    android:layout_height="2dip"
    android:background="#FF909090" />
</TableLayout>

</LinearLayout>
</ScrollView>
supersam654
  • 3,126
  • 33
  • 34
user1798956
  • 53
  • 4
  • 12

4 Answers4

0

You just need to follow "Factory Pattern". Pick and example from anywhere and modify your code accordingly. You can write an abstract factory class and can extend as per your need.

Sushil
  • 8,250
  • 3
  • 39
  • 71
0

What about making one activity with a WebKit browser and html pages inside? You could have a whole 'Gameopaedia' there with list of creatures, factions whatever. Maybe you could get over with just one activity for this.

Then, if you have just about 3 screens Statistics, FunFacts and Home and if they display the same data, I would make just 3 activities and parametrize them somehow.

Check out this answer for parameter passing to a launched activity.

Community
  • 1
  • 1
nio
  • 5,141
  • 2
  • 24
  • 35
  • Well the issue is, I don't think I could use HTML pages inside because all of the information on the creatures are in TextView's in the 20-something XML files. So I don't think an HTML view would help. Isn't there just a way to have one Class and one Layout file and depending on which creature I clicked on, it would change the TextView's in that one Layout file to match the creature I clicked? There has to be some way to do that! – user1798956 Aug 05 '13 at 08:15
0

To make your xml files reusable first of all you should take away all the hard coded strings like:

android:text="Bats sleep during the day, and become active at night. While idle, a bat will hang upside down until a player approaches, which it will then fly away. They cannot hang on non-solid or transparent blocks. If you place a bat in a minecart, it will move the minecart on its own."

You should put strings like that in a database or , if u don't want a db, in the strings.xml file u can find in yourProject/res/values. Doing this your xml file will be a void structure reusable to display many creature class. Then u will inject dynamically, from the Bat.java class, the appropriate strings in the creatures.xml using method like setText(String string).

For the reusability of the java class the problem is more complex because i really don't know what you have in mind. I suggest you to use an abstract class that will implements all the methods that are common for all your class like:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String userTheme = prefs.getString("theme", "main");

    if (userTheme.equals("main"))
        setTheme(R.style.MainTheme);
    else if (userTheme.equals("light"))
        setTheme(R.style.HoloLight);
    else if (userTheme.equals("lightdark"))
        setTheme(R.style.HoloLightDark);
    else if (userTheme.equals("dark"))
        setTheme(R.style.HoloTheme);
    setContentView(R.layout.creature_info_structure);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    inflater.inflate(R.menu.options, menu);
    return true;
}

Going further and trying to design your own project is impossible for me. The only thing i can do is suggest you to read something about Polymorphism and Inheritance, two principle of the OO programming that will help you a lot to design and develop your project. Hope i put you in the right direction.

0

I would just have a local database (or simply a local JSON file) with the data for each creature. Then, create two fragments. One of them will be your master fragment and the other one will be your details fragment.

For the master fragment, you'd probably want to subclass ListFragment so that you get the list functionality. Then make an ArrayAdapter to feed the creatures from your database into the list fragment.

For the details fragment, create the fields that will show the details of the selected creature. Now here's a neat trick: since these are fragments, you can optimize your UI for phones and tablets. On phones, you show only the master fragment at first, and wait until the user clicks to swap it with the details fragment. Now, on a tablet, you show the master fragment and the details fragment side-by-side: the user can select a creature on the left and see the details on the right.

When the user selects a creature (regardless of whether you are on a phone or tablet), you populate the details fragment with the data for the selected creature. You do that by loading your JSON or database entry, then set the content for each widget.

For details, be sure to take a look at this training class: Building a Dynamic UI with Fragments.

Bruno Oliveira
  • 5,056
  • 18
  • 24