2014-11-22 7 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")); 

에서 위치를 얻을 수있는 방법 // 나는 등 1, 2로 정적 위치를 원하는이 배열내가 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(); 



      } 

     }); 

    } 
} 

에서 위치를 원한다. 그러나 나는 그것을 얻지 못하고있다.

+0

하는 클릭 한 항목의 위치 또는 데이터를 클릭하는 것과 같은 위치가 필요합니까? –

+0

격자보기를 클릭하면 위치 번호를 MainActivity.class로 보내려고합니다. –

+0

GridActivity의 MainActivity에 대한 데이터를 표시하려면 내 ans 및 what을 (를) 확인하십시오. –

답변

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); 
     } 
}); 
관련 문제