Java 如何从ArrayList获得职位<&燃气轮机; /** *由myozawoo于2014年11月21日创建。 */ 公共类GridActivity扩展了活动{ GridView; ArrayList gridArray=新的ArrayList(); CustomGridViewAdapter customGridAdapter; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); requestWindowFeature(窗口。功能\u无\u标题); getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,0); setContentView(R.layout.activity_网格); //设置网格视图项 最终位图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); 添加(新项(一个,“文本一”); 添加(新项(两个,“文本二”); 添加(新项(三,“文本三”); 添加(新项(四,“文本四”); 添加(新项(五,“文本五”); 添加(新项(六,“文本六”);

Java 如何从ArrayList获得职位<&燃气轮机; /** *由myozawoo于2014年11月21日创建。 */ 公共类GridActivity扩展了活动{ GridView; ArrayList gridArray=新的ArrayList(); CustomGridViewAdapter customGridAdapter; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); requestWindowFeature(窗口。功能\u无\u标题); getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,0); setContentView(R.layout.activity_网格); //设置网格视图项 最终位图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); 添加(新项(一个,“文本一”); 添加(新项(两个,“文本二”); 添加(新项(三,“文本三”); 添加(新项(四,“文本四”); 添加(新项(五,“文本五”); 添加(新项(六,“文本六”);,java,android,arraylist,android-studio,Java,Android,Arraylist,Android Studio,//我想要这个数组中的位置 /** * Created by myozawoo on 11/21/14. */ public class GridActivity extends Activity { GridView gridView; ArrayList<Item> gridArray = new ArrayList<Item>(); CustomGridViewAdapter customGridAdapter; @Overr

//我想要这个数组中的位置

/**
 * 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"));
gridView=(gridView)findViewById(R.id.gridView);
customGridAdapter=新的CustomGridViewAdapter(此,R.layout.custom_单元格,gridArray);
setAdapter(customGridAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长l){
Intent Intent=new Intent(getBaseContext(),MainActivity.class);
intent.putExtra(“page”,gridArray.get(position.toString());
星触觉(意向);
Toast.makeText(getApplicationContext(),gridArray.get(position).toString(),
吐司。长度(短)。show();
}
});
}
}

我想要静态的位置,比如1,2。但我不明白。

在Item类中再添加一个字符串字段页:

        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();



            }

        });

    }
}
在构造函数中添加页值:

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(新的AdapterView.OnItemClickListener(){
@凌驾
公共虚线单击(AdapterView AdapterView,视图视图,内部位置,长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());
星触觉(意向);
}
});

您需要哪个位置,如哪个项目单击位置或单击项目中的数据?如果我单击网格视图,我想将位置号发送到MainActivity.class。请检查我的ans和您想要显示GridActivity中MainActivity数据的内容?
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);
      }
});