1

So I created a custom ListView with 2 objects in the list. For example, the first on the list is Cats and the second is Dogs. Then when one is tapped, it will navigate to a different activity screen. So for example, when the user taps "Dogs", it will go to a different screen with a new list with names on the list such as "Corgi", "Pug", "Husky", etc. Then when a dog breed is tap, it will forward to a website. For example, when the user taps "Corgi", it will forward the user to Corgi's Wiki page.

How will I go about creating the third part of the app?

My code is the following:

MainActivity:

    public class MainActivity extends ListActivity implements OnItemClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] data = { "Dogs", "Cats" };
        int[] icons = { R.drawable.dogz, R.drawable.catz };

        // Provide the cursor for the list view. 
        setListAdapter(new CustomListAdapter(this, data, icons));

        /* setOnItemClickListener() Register a callback to be invoked when an item 
         * in this AdapterView has been clicked.
         */
        getListView().setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Intent intent = new Intent(parent.getContext(), ChildActivity.class);

        // Add extended data to the intent.
        intent.putExtra("POSITION", position);

        /*
         * Launch a new activity. You will not receive any information about when 
         * the activity exits. This implementation overrides the base version, 
         * providing information about the activity performing the launch.
         */
        startActivity(intent);
    }

}

ChildActivity:

    public class ChildActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[][] data = {
                { "Corgi", "Pugs", "Husky" },
                { "Siamese", "Persian", "Maine Coon" } };
        int[][] icons = {
                { R.drawable.d, R.drawable.e, R.drawable.f },
                { R.drawable.a, R.drawable.b, R.drawable.c }, };
        Intent intent = getIntent();
        int position = intent.getIntExtra("POSITION", 0);

        // Provide the cursor for the list view. 
        setListAdapter(new CustomListAdapter(this, data[position],
                icons[position]));

    }

}

CustomListAdapter:

public class CustomListAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final String[] values;
    private final int[] icons;

    public CustomListAdapter(Context context, String[] values, int[] icons) {
        super(context, R.layout.row_layout, values);
        this.context = context;
        this.values = values;
        this.icons = icons;
    }

    @Override
    // Get a View that displays the data at the specified position in the data set.
    public View getView(int position, View convertView, ViewGroup parent) {

        /*
         * Instantiates a layout XML file into its corresponding View objects. 
         * It is never used directly. Instead, use getSystemService(String) to 
         * retrieve a standard LayoutInflater instance that is already hooked up to
         * the current context and correctly configured for the device you are running on.
         */
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        /*
         * Inflate a new view hierarchy from the specified xml resource. 
         * Throws InflateException if there is an error.
         */
        View rowView = inflater.inflate(R.layout.row_layout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.txtStatus);
        textView.setText(values[position]);
        Drawable draw = context.getResources().getDrawable(icons[position]);
        Bitmap bitmap = ((BitmapDrawable) draw).getBitmap();
        int h = bitmap.getHeight();
        int w = bitmap.getWidth();
        Drawable newDraw = new BitmapDrawable(context.getResources(),
                Bitmap.createScaledBitmap(bitmap, 40 * w / h, 40, true));

        /*
         * Sets the Drawables (if any) to appear to the left of, above, to the right of,
         *  and below the text. Use 0 if you do not want a Drawable there. 
         * The Drawables' bounds will be set to their intrinsic bounds.
         */
        textView.setCompoundDrawablesWithIntrinsicBounds(newDraw, null, null,
                null);
        return rowView;
    }
}

I know I would have to create a third activity but I do not know how to go about it without using the .xml. Thank in advance.

Wooster
  • 13
  • 4
  • I wouldn't add a new activity - look at launching an intent: http://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application – russianmario Mar 01 '16 at 19:44
  • @russianmario If I ignore the third activity, and add the "browserIntent" code. How will I make each url special to each breed? – Wooster Mar 01 '16 at 19:54
  • Instead of creating an array of Strings to list each breed, you would probably want to create a class Breed that contains the breed name and the URL you want to go to, then create a List. You can continue to use the ArrayAdapter (with some modifications to use the Breed object). Then use the `onItemClick` event: http://stackoverflow.com/a/16746579/3163097 – russianmario Mar 01 '16 at 19:59
  • @russianmario, I definitely want to keep the strings array. But instead of an url, how about just a paragraph about the breed? "Dog" -> "Corgi" -> Details about the breed Corgi – Wooster Mar 01 '16 at 20:08
  • Same deal, I would recommend a class to contain the breed name and description. And actually, I would also recommend making the icon part of that class - the two arrays of breed names and icons are not maintainable. – russianmario Mar 01 '16 at 20:12
  • 1
    @russianmario, Thanks. I will implement your recommends when I have the chance and will give you an update. – Wooster Mar 01 '16 at 20:15

1 Answers1

0
  1. Try making an Adapter with Animal items.
  2. OnItemClick() open a WebViewFragment and pass it either your Animal Object or the Animal.getUrl()-String in a Bundle
  3. (optional) Check out ExpandableListView to prevent unnecessary deep Navigation Hierarchies.
Flo We
  • 325
  • 2
  • 12