Frage

https://itunes.apple.com/us/app/party-cupcake-recipes-300+/id572480130?mt=8

I want to make an application like the example I provided above. I am doing it at Android and with Java. I am using a listview for main menu, it has soups, main dishes, etc.. I constructed an activity for each item at the menu and at click of the user it opens this new activity.

In example, soup activity has another listview in it and it will have a list of soups I have made and the recipe will be an imageview which I construct with photoshop and there will be no text or something.

Now my problem is I will probably have 50-60 recipes and it does not sound logical to make a class for each of them. With OOP logic I should have a class such as recipe and produce objects with the name of the recipe and the imageview of it. So I can produce hundreds of them if I need and this does sound logical. I am thinking about storing all the pictures at the resources folder. When I do that can I give a link to the picture only without adding it to an xml file. I mean can I do a listview with an Arraylist which consists of recipe objects and when one clicks on them it opens the image it has. Without having an xml file for all of them or is it not possible at all? All the help would be appreciated...

War es hilfreich?

Lösung

I don't know what do you mean by "xml file for all of them" but you can get the picture by BitmapFactory.decodeResource(getResources(), R.drawable.pictureId) and show that bitmap in ImageView.

public class MySimpleArrayAdapter extends ArrayAdapter<MyImage> {
  private final Context context;
  private final ArrayList<MyImage> values;

  public MySimpleArrayAdapter(Context context, ArrayList<MyImage> values) {
    super(context, R.layout.rowlayout, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values.get(i).getDesc());
    imageView.setImageResource(values.get(position).getImageId());

    return rowView;
  }
} 

public class MyImage{
   String desc;
   int imageId;
}


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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20px" >
    </TextView>

</LinearLayout> 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top