2014-09-21 126 views
2

即時通訊嘗試創建一個列表視圖,如果您按下該列表上的項目,它會以全屏顯示圖像。我在列表中有大約10個不同的項目,我想爲每個項目顯示不同的圖像。點擊列表查看項目和顯示圖像

我用這個教程:http://www.androidhive.info/2011/10/android-listview-tutorial/

但是,而不是顯示在我的字符串項的名稱,我想讓它顯示的圖像。我該如何解決?

我一直在試圖檢查其他問題,但他們有點難以遵循,因爲即時通訊新的這一點。

+0

你必須在onItemClickListener裏面開始一個新的活動 – joao2fast4u 2014-09-21 19:39:29

回答

0

所有你需要有圖片爲所有不同的項目(在列表視圖)您可繪製文件夾的第一 的single_list_item.xml更改爲

single_list_item_view.xml 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView android:id="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      />  
</LinearLayout> 

改變你的SingleListItem。 java

package com.androidhive.androidlistview; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.widget.TextView; 

    public class SingleListItem extends Activity{ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      this.setContentView(R.layout.single_list_item_view); 

      ImageView productimage = (ImageView) findViewById(R.id.image); 

      Intent i = getIntent(); 
      // getting attached intent data 
      String product = i.getStringExtra("product"); 
      // displaying selected product name 
      switch(product){ 
      case "item1": 
       productimage.setImageDrawable(R.drawable.image1); 
      break; 
      case "item2": 
       productimage.setImageDrawable(R.drawable.image2); 
      break; 
      case "item3": 
       productimage.setImageDrawable(R.drawable.image3); 
     break; 
      . 
      . 
      . 
      . 
      . 
//upto all 10 images 

     } 

     } 
    } 
+1

這段代碼有一些錯誤。 'setImageDrawable'需要一個Drawable參數,而不是一個整數ID。它應該是'setImageResource'。 switch語句也有不正確的語法:'case:item1'應該是'case item1:'。另外,Strings只能在Java 7及更高版本的switch語句中使用,並且這些case中的值必須是常量字面值。 – 2014-09-21 20:10:16

+0

@Matt Giles感謝您注意到錯誤。 – Harsha 2014-09-21 20:33:47

+0

感謝您的幫助,我得到了它的工作。不知何故,我結合了你的想法或某事。不知道我是如何設法修復它的。起初我嘗試了兩種方法,沒有人工作。然後,我只是搞砸了,並得到它的工作!非常感謝! – Rasmus 2014-09-21 21:00:51

0

而不是顯示SingleListItem類中的項目/文本,您可以使用傳遞的項目來檢查應該在圖像視圖中顯示哪個圖像。類似的東西:

public class SingleListItem extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.single_list_item_view); 

     // TextView txtProduct = (TextView) findViewById(R.id.product_label); 
     ImageView image = (ImageView) findViewById(R.id.someImage); 

     Intent i = getIntent(); 
     // getting attached intent data 
     String product = i.getStringExtra("product"); 
     // displaying selected product name 
     if (product.equals("item1")) { 
      image.setDrawableResource(R.drawable.YOURIMAGE); 
     } else if (product.equals("item2")) 
      image.setDrawableResource(R.drawable.ANOTHERIMAGE); 

    } 
} 
0

只有對教程代碼進行一些更改,您應該可以做到這一點。

首先,在single_list_item_view.xml中,您需要使用ImageView而不是TextView,因爲您要顯示圖像。您可以查找圖像視圖的文檔,包括它們支持的屬性,on the Android developer site

其次,您需要更改查找TextView的行以找到新的ImageView。這將只需要更改以下行:

TextView txtProduct = (TextView) findViewById(R.id.product_label); 

到:

// Assuming you gave your ImageView the id "product_image" in single_list_item_view.xml 
ImageView productImage = (ImageView) findViewById(R.id.product_image); 

你需要添加你想顯示到你的項目中的圖像。最簡單的地方是res/drawable文件夾。圖像文件的名稱將決定其「ID」。例如,如果您有res/drawable/photoshop.png,則可以使用編號R.drawable.photoshop來引用它。

接下來,您需要弄清楚要在SingleListItem中顯示哪個圖像。如何在列表中的項目點擊的關鍵部分顯示別的東西是SingleListItem

Intent i = getIntent(); 
String product = i.getStringExtra("product"); 

相應地,在AndroidListViewActivity

Intent i = new Intent(getApplicationContext(), SingleListItem.class); 
i.putExtra("product", product); 
startActivity(i); 

Intent是如何從列表中的活動信息發送到顯示項目的活動。在這種情況下,發送的信息是要顯示的項目。你有幾個關於如何做這部分的選項。一個簡單的方式就是檢查product字符串,並選擇適當的繪製:

int imageId = -1; 
if (product.equals("Adobe After Effects")) { 
    imageId = R.drawable.after_effects; 
} else if (product.equals("Adobe Bridge")) { 
    imageId = R.drawable.bridge; 
} else if ... 
    ... // All your other cases 
} 

這種做法顯然不規模非常好,但它的工作好爲您簡單的例子。另一種可能的方法是通過圖片ID在Intent,而不是產品的名稱,例如:

// In AndroidListViewActivity: 
i.putExtra("product", R.drawable.after_effects); 

// In SingleListItem: 
int imageId = i.getIntExtra("product", -1); 
// -1 in the call above is the value to return if "product" is not in the Intent 

你需要做的最後一件事是設置Drawable您先前發現的ImageView

// Make sure to check that the id is valid before using it 
if (imageId != -1) { 
    Drawable d = getResources().getDrawable(imageId); 
    productImage.setDrawable(d); 
} 

getResources()獲取Resources對象爲您的應用程序。您可以使用Resources對象來查找您在res文件夾中定義的字符串和繪圖。您正在關注的教程也使用它在res文件夾中查找字符串數組。再次,您可以在Android developer site上看到所有支持的Resources

+0

爲什麼行:「int imageId = -1;」以上:if(product.equals(「Adobe After Effects」)){imageId = R.drawable.after_effects;其他if(product.equals(「Adobe Bridge」)){imageId = R.drawable.bridge; } else if ... ... //你所有的其他案例 } 它似乎爲我生成一個錯誤 – Rasmus 2014-09-24 16:31:47