2013-12-22 89 views
0

我有一個簡單的ListView與ImageView和textView。顯示ListView項目(ImageView)到另一個活動

現在,每當我點擊我的listItem我需要將ImageView發送到另一個活動。

我讀過很多SO答案,但找不到更好的解決方案。

這是我lisview_items.xml:

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

    <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 
    <ImageView 
     android:id="@+id/photo" 
     android:layout_width="150dip" 
       android:layout_height="100dip" 
     android:paddingRight="15dp" 
     android:paddingLeft="15dp"/> 
    </LinearLayout> 
    <RelativeLayout 
    android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:layout_marginLeft="160dp" 
       android:layout_marginTop="10dp" 
       android:descendantFocusability="blocksDescendants"> 
    <TextView android:id="@+id/name" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:textColor="#000000" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 
<RelativeLayout> 

CustomAdapter:

public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = l_Inflater.inflate(R.layout.item_details_view, null); 
     holder = new ViewHolder(); 
     holder.txt_itemName = (TextView) convertView.findViewById(R.id.name); 
     holder.itemImage = (ImageView) convertView.findViewById(R.id.photo); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName()); 
      holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]); 
} 

MyListView的OnClick:

ArrayList<ItemDetails> image_details = GetSearchResults(); 

    final ListView lv1 = (ListView) findViewById(R.id.list); 
    lv1.setAdapter(new ItemListBaseAdapter(this, image_details)); 


    OnItemClickListener itemClickListener = new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 

      Object o = lv1.getItemAtPosition(position); 
      ItemDetails obj_itemDetails = (ItemDetails)o; 
      Toast.makeText(ListViewImagesActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show(); 
      ProductName =obj_itemDetails.getName(); 
     } 
    }; 

我使用sharedpreferences發送產品名稱如下圖所示,它作品絕對精細

SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
data.edit().putString("SelectedProduct", ProductName).commit(); 

在類似的方式如何發送圖像,因爲它是在ArrayList中所示above..Can人流下了我一些光該使用

回答

0

您可以將圖像路徑存儲ItemDetails的物體,像你正在做這個名字。然後您可以通過Intent將路徑傳遞給新的Activity

如果圖片在編譯時可用,您可以傳遞一個引用到它的id,就像Gabe說的,而不是它的位置路徑。

0

如果您實際切換到該活動,則可以在意圖附加項中傳遞圖像的資源ID,然後在那裏使用getResources().getDrawable(ID)

0

你應該如下:

final ArrayList<ItemDetails> image_details = GetSearchResults(); 

final ListView lv1 = (ListView) findViewById(R.id.list); 
lv1.setAdapter(new ItemListBaseAdapter(this, image_details)); 


OnItemClickListener itemClickListener = new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> a, View v, int position, long id) { 

     ItemDetails selectedItem = image_details.get(position); 
     //To-DO get you data from the ItemDetails Getter 
     // selectedItem.getImage() or selectedItem.getName() .. etc 
     // the send the data using intent when opening another activity 
     Intent intent = new Intent(CurrentActivity.this, DisplayImageActivity.class); 
     intent.putExtra("image",selectedItem.getImage()); 
     intent.putExtra("name",selectedItem.getName()); 
     startActivty(intent); 

    } 
}; 
在DisplayImageActivity

然後在OnCreate

@Override 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Intent i = getIntent(); 
     String image = i.getStringExtra("image"); 
     String name = i.getStringExtra("name"); 

} 
相關問題