2013-03-08 27 views
0

https://itunes.apple.com/us/app/party-cupcake-recipes-300+/id572480130?mt=8在Android的製作配方的應用程序使用列表視圖圖片

我想要像我上面提供的示例應用程序。我正在使用Android和Java。我使用主菜單的列表視圖,它有湯,主菜等。我爲菜單中的每個項目構建了一個活動,並在用戶點擊時打開了這個新活動。

在例子中,湯活動有另一個列表視圖,它會有一個我已經制作的湯的列表,配方將是我用photoshop構建的imageview,並且不會有文本或其他東西。

現在我的問題是我可能會有50-60個食譜,並且聽起來不合邏輯的爲他們每個人做一個類。使用OOP邏輯,我應該有一個類,如配方,並生成具有配方名稱和圖像視圖的對象。所以如果我需要的話,我可以生成數百個,這聽起來合乎邏輯。我正在考慮將所有圖片存儲在資源文件夾中。當我這樣做時,我只能給圖片鏈接,而不會將其添加到xml文件。我的意思是我可以用一個由配方對象組成的Arraylist進行列表視圖,當點擊它們時它會打開它的圖像。沒有一個XML文件的所有人,或者根本不可能?所有的幫助,將不勝感激...

+0

你問關於如何自己存儲圖片嗎?或者如何引用圖片及其路徑(不使用XML)。您需要使用某些東西,最好是已存儲所有配方名稱的任何東西,也可以將它們的路徑存儲在資源文件夾(無論是XML還是一些簡單的數據庫)中。 – Wrightboy 2013-03-08 22:43:44

+0

我在問兩個問題。我可以將它們存儲在資源文件夾中,只需將它們的路徑從列表視圖打開到屏幕即可。或者我必須爲所有圖像構建xml頁面。 – curiousprogrammer 2013-03-09 00:26:31

回答

0

我不知道你是什麼意思的「所有他們的XML文件」,但你可以通過BitmapFactory.decodeResource(getResources(),R.drawable得到的圖片。 pictureId)並在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> 
+0

我想說如果你可以顯示一個圖像,而不需要將它添加到帶有listview的xml文件中。 – curiousprogrammer 2013-03-08 22:46:22

+0

您可以將圖像id存儲在ArrayList中,並獲取您在ListView上單擊的位置,然後將id發送到Activity並顯示圖像。 – 2013-03-08 23:02:52

+0

我不明白你想達到什麼。你不必爲圖像製作xml。只需將它們存儲在資源文件夾中並使用BitmapFactory創建位圖即可。檢查我的編輯 – 2013-03-09 10:04:54

相關問題