2014-03-03 67 views
0

我'新的Android越來越像ID,我已經做圖像的網格視圖的項目的onclick上的圖像就說明它,多數民衆贊成在ImageAdapter類代碼從ImageAdapter

public class ImageAdapter extends BaseAdapter{ 

    private static LayoutInflater inflater = null; 
    private Activity activity; 

    private String mode = ""; 

    int[] images=null ; 

    public ImageAdapter(Activity act, String mode , int[] images){ 
     inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     activity = act; 
     this.mode = mode; 
     this.images= images ; 
    } 

    public ImageView getImage(int pos) 
    { 
     ImageView im = (ImageView) getItem(pos); 
     return im ; 
    } 


    @Override 
    public int getCount() { 
     return images.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return new Integer(images[position]); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     if(mode.equalsIgnoreCase("grid")){ 
      if (view == null) { 
       view = inflater.inflate(R.layout.each_image1, null); 
      } 
      ImageView iv = (ImageView)view.findViewById(R.id.imageView); 
      iv.setImageResource(images[position]); 
     } else if(mode.equalsIgnoreCase("gallery")){ 
      if (view == null) { 
       view = inflater.inflate(R.layout.each_image_gallery, null); 
      } 
      ImageView iv = (ImageView)view.findViewById(R.id.imageView); 
      iv.setImageResource(images[position]); 
     } 
     return view; 
    } 

} 

這就是我的網格活動

public class GridActivity extends Activity { 


    GridView grid = null; 
    public static ImageAdapter adapter1 ; 
    public static ImageAdapter adapter2 ; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.grid); 
     adapter1= new ImageAdapter(GridActivity.this, "grid" , Images.images1); 
     adapter2 = new ImageAdapter(GridActivity.this, "grid" , Images.images2); 

      Intent i = this.getIntent(); 
     if (i!=null) 
     { 
     String unique = i.getExtras().getString("Unique"); 
     if (unique.equals("islam")) 

       { 
     Toast.makeText(GridActivity.this, "islam", Toast.LENGTH_LONG).show(); 


     grid = (GridView)findViewById(R.id.gridView1); 
     grid.setAdapter(adapter1); 

       } 
     if (unique.equals("natural")) 
     { 
      Toast.makeText(GridActivity.this, "nat", Toast.LENGTH_LONG).show(); 


      grid = (GridView)findViewById(R.id.gridView1); 
      grid.setAdapter(adapter2); 
     } 

     } 

     grid.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long id) { 
       Intent i = new Intent(GridActivity.this, imgPrevActivity.class); 
       i.putExtra("selectedIntex", pos); 
       startActivity(i); 

       Toast.makeText(GridActivity.this,"ddd",Toast.LENGTH_LONG).show(); 


      } 
     }); 
    } 
} 

,這是它假定顯示任何點擊圖像

public class imgPrevActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.each_image1); 
     ImageView im = (ImageView)findViewById(R.id.imageView); 
     int pos = getIntent().getExtras().getInt("selectedIntex"); 

     // ImageAdapter adapter = new ImageAdapter(imgPrevActivity.this, "image prev", null); 

     long Id= GridActivity.adapter1.getItemId(pos); 
     im.setImageResource((int) Id); 

    } 

} 

我三活動編輯以獲取從網格 點擊圖像的位置,然後從位置 獲得Id並將圖像視圖設置爲該ID

但它不工作!

多數民衆贊成在圖像放入數組

public class Images { 

    public static int[] images1 = { 
     R.drawable.buds, R.drawable.cherry_34, 
     R.drawable.clouds_2, R.drawable.coffee_beans_2, 
     R.drawable.death_valley_sand_dunes 
     }; 

    public static int[] images2= { 
     R.drawable.morning_glory_pool, 
     R.drawable.pink_flowers, R.drawable.sun_flower, 
     R.drawable.sunrise_3, R.drawable.yellow_rose_3, 



    }; 





} 
+0

你是什麼意思,但它不工作!任何錯誤? –

+0

@Tanimreja 什麼也沒有顯示。空白活動 – Fawzinov

回答

0

你必須意圖通過你的圖像資源ID。適配器的列表項ID無用,因爲您在屏幕上有新的活動。

grid.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long id) { 
      Intent i = new Intent(GridActivity.this, imgPrevActivity.class); 
      i.putExtra("selectedIntex", grid.getAdapter().getItem(pos); 
      startActivity(i); 

      Toast.makeText(GridActivity.this,"ddd",Toast.LENGTH_LONG).show(); 


     } 
    }); 

,並改變你ImgPreviewActivity代碼:

public class imgPrevActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.each_image1); 
    ImageView im = (ImageView)findViewById(R.id.imageView); 
    int pos = getIntent().getIntExtra("selectedIntex", 0); 
    im.setImageResource(pos); 
} 

}

,我認爲它應該工作

相關問題