2014-11-22 49 views
0
/** 
* Created by myozawoo on 11/21/14. 
*/ 
public class GridActivity extends Activity { 

    GridView gridView; 
    ArrayList<Item> gridArray = new ArrayList<Item>(); 
    CustomGridViewAdapter customGridAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0); 

     setContentView(R.layout.activity_grid); 

     //set grid view item 
     final Bitmap one = BitmapFactory.decodeResource(this.getResources(), R.drawable.one); 
     Bitmap two = BitmapFactory.decodeResource(this.getResources(), R.drawable.two); 
     Bitmap three = BitmapFactory.decodeResource(this.getResources(), R.drawable.three); 
     Bitmap four = BitmapFactory.decodeResource(this.getResources(), R.drawable.four); 
     Bitmap five = BitmapFactory.decodeResource(this.getResources(), R.drawable.five); 
     Bitmap six = BitmapFactory.decodeResource(this.getResources(), R.drawable.six); 

     gridArray.add(new Item(one,"Text One")); 
     gridArray.add(new Item(two,"Text Two")); 
     gridArray.add(new Item(three,"Text Three")); 
     gridArray.add(new Item(four,"Text Four")); 
     gridArray.add(new Item(five,"Text Five")); 
     gridArray.add(new Item(six,"Text Six")); 

//我想要的位置從該陣列我怎樣才能獲得位置從ArrayList的<>

 gridView = (GridView) findViewById(R.id.gridView); 
     customGridAdapter = new CustomGridViewAdapter(this, R.layout.custom_cell, gridArray); 
     gridView.setAdapter(customGridAdapter); 

     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
       Intent intent = new Intent(getBaseContext(), MainActivity.class); 
       intent.putExtra("page", gridArray.get(position).toString());     
       startActivity(intent); 

       Toast.makeText(getApplicationContext(), gridArray.get(position).toString(), 
         Toast.LENGTH_SHORT).show(); 



      } 

     }); 

    } 
} 

欲如1,2-靜態位置。但我沒有得到它。

+0

這你需要哪個位置點擊位置或點擊項目中的數據? – 2014-11-22 09:24:32

+0

如果我點擊網格視圖,我想發送位置編號到MainActivity.class。 – 2014-11-22 09:34:09

+0

請檢查我的答案和你想在GridActivity上顯示MainActivity的數據? – 2014-11-22 09:35:53

回答

0

項類再添加一個字符串字段頁面:在構造函數中

public class Item { 
    private String text; 
    private Bitmap image; 
    private String page; 


    public Item(Bitmap image,String text,String page){ 
     this.image = image; 
     this.text = text; 
     this.page = page; 
    } 

    public Bitmap getImage() { 
     return image; 
    } 


    public String getText() { 
     return text; 
    } 


    public String getPage() { 
     return page; 
    } 
} 

加頁值:

gridArray.add(new Item(one,"Text One","one")); 

獲取從項目相應頁面值時點擊:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
     Toast.makeText(getApplicationContext(),gridArray.get(position).getPage(),Toast.LENGTH_SHORT).show(); 
     Intent intent = new Intent(getBaseContext(), MainActivity.class); 
     intent.putExtra("page", gridArray.get(position).getPage());     
     startActivity(intent); 
     } 
});