1

我想全屏显示图像的完整活动类。我在 gridView 中显示了所有图像。我没有遇到问题,但是当我单击 gridview 的图像时,它只显示了我调用的 xml 文件。我想我在 FullImageAcitivity 文件中调用 id 的地方做错了。

public class FullImageActivity extends Activity {

Button download, setas;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);
    setas = (Button) findViewById(R.id.setas);
    download = (Button)findViewById(R.id.download);
    final Intent i = getIntent();
    final   List<Item> items = new ArrayList<Item>();
    final ImageAdapter imageAdapter = new ImageAdapter(this);       
    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(items.indexOf(i));
    setas.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             WallpaperManager myWallpaperManager
             = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(imageAdapter.items.indexOf(i));
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
    }
    });
  }
}

这是我的 imageAdapter 文件。

public class ImageAdapter extends BaseAdapter {
List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;

public ImageAdapter(Context context) {
    inflater = LayoutInflater.from(context);

    items.add(new Item("One",       R.drawable.abstact_one));
    items.add(new Item("Two",   R.drawable.abstract_three));
    items.add(new Item("Three", R.drawable.image_two));
    items.add(new Item("Four",      R.drawable.image_four));
    items.add(new Item("Five",     R.drawable.image_five));
    items.add(new Item("Six",      R.drawable.image_nine));
    items.add(new Item("Seven",       R.drawable.image_ten));
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int i) {
    return items.get(i);
}

@Override
public long getItemId(int i) {
    return items.get(i).drawableId;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;
    TextView name;

    if(v == null) {
        v = inflater.inflate(R.layout.other, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
        v.setTag(R.id.text, v.findViewById(R.id.text));
    }

    picture = (ImageView)v.getTag(R.id.picture);
    name = (TextView)v.getTag(R.id.text);

    Item item = (Item)getItem(i);

    picture.setImageResource(item.drawableId);
    name.setText(item.name);

    return v;
}

private class Item {
    final String name;
    final int drawableId;

    Item(String name, int drawableId) {
        this.name = name;
        this.drawableId = drawableId;
    }
 }
}

latestTab 活动文件

public class LatestTab extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.latestphotos);

      GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));

        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });

}
}

full_image.xml

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

<RelativeLayout
    android:id="@+id/relshare"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#79B8B8B8" >

    <Button
        android:id="@+id/setas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/share"
        android:gravity="right"
        android:text="Set As"
        android:textSize="25sp" />

    <Button
        android:id="@+id/download"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="34dp"
        android:text="Download"
        android:textSize="25sp" />

</RelativeLayout>

<ImageView android:id="@+id/full_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

最新照片.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:numColumns="2" 
    />
</FrameLayout>
4

2 回答 2

0

在你的LatestTab

将列表声明为类成员

  List<Item> items = new ArrayList<Item>();

将项目添加到列表中

items.add(new Item("One", R.drawable.abstact_one));
items.add(new Item("Two",  R.drawable.abstract_three));
items.add(new Item("Three", R.drawable.image_two));
items.add(new Item("Four", R.drawable.image_four));
items.add(new Item("Five", R.drawable.image_five));
items.add(new Item("Six",  R.drawable.image_nine));
items.add(new Item("Seven", R.drawable.image_ten));

你的物品类别

public class Item {
String name;
int drawable;
public int getDrawable() {
    return drawable;
}
public void setDrawable(int drawable) {
    this.drawable = drawable;
}
public Item(String name, int id)
{
    this.name= name;
    this.drawable = id;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

将列表传递给适配器类的构造函数

 gridView.setAdapter(new ImageAdapter(this,items));

在构造函数中

  List<Item> items;
  public ImageAdapter(Context context,List<Item> items) {
   inflater = LayoutInflater.from(context);
   this.items = items;
 } 

然后在 getView

 Item item = (Item)items.get(i);
 picture.setImageResource(item.getDrawable());

然后在项目点击监听器

  Item item = items.get(position);
  int  id = item.getDrawable(); 
  Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
  i.putExtra("idkey", id); // pass the id
  startActivity(i);

然后在 FullImageActivity

  int id = getIntent().getIntExtra("idkey"); //get id
  imageview.setImageResource(id); // set the drawable to imageview
于 2013-10-23T12:43:52.840 回答
0

它仅在FullImageActivity中显示布局,因为您已将值传入Intent但实际上并未在 FullImageActivity 中获取它。

在您的活动中获得如下值:

  final int i = getIntent().getIntExtra("id");

而且在您的ArrayList您没有添加任何值,这就是为什么它无法i从您的item列表中获取值的原因。只需在您的值中添加items如下:

      // Selected image id
final   List<Item> items = new ArrayList<Item>();
items.add(new Item("One",       R.drawable.abstact_one));
items.add(new Item("Two",   R.drawable.abstract_three));
items.add(new Item("Three", R.drawable.image_two));
items.add(new Item("Four",      R.drawable.image_four));
items.add(new Item("Five",     R.drawable.image_five));
items.add(new Item("Six",      R.drawable.image_nine));
items.add(new Item("Seven",       R.drawable.image_ten));

之后将图像设置为ImageView

ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(items.indexOf(i));
于 2013-10-23T13:12:42.830 回答