将ListView Item(ImageView)显示给另一个活动

时间:2013-12-22 19:14:57

标签: android listview android-activity imageview transfer

我有一个带有ImageView和textView的简单列表视图。

现在每当我点击我的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 data = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
data.edit().putString("SelectedProduct", ProductName).commit();

以类似的方式,如何将图像发送到ArrayList中,如上所示。任何人都可以告诉我这个用法的一些说明

3 个答案:

答案 0 :(得分:0)

您可以将图像路径存储在ItemDetails对象上,就像使用名称一样。然后,您可以通过Intent将路径传递给新的Activity

如果图片在编译时可用,你可以像Gabe所说的那样传递对id的引用,而不是它的位置路径。

答案 1 :(得分:0)

如果您实际切换到该活动,则可以在intent extras中传递图像的资源ID,然后在那里使用getResources().getDrawable(ID)

答案 2 :(得分: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);

   }  
};

然后在OnCreate的DisplayImageActivity中

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

}