【问题标题】:How can I get Position from ArrayList<>如何从 ArrayList<> 获取位置
【发布时间】:2014-11-22 09:10:08
【问题描述】:
/**
 * 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 = 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。但我没听懂。

【问题讨论】:

  • 您需要哪个位置,例如哪个项目点击位置或点击项目的数据?
  • 如果我单击网格视图,我想将位置编号发送到 MainActivity.class。
  • 请检查我的答案以及您希望从 GridActivity 中显示 MainActivity 数据的内容?

标签: java android arraylist android-studio


【解决方案1】:

在Item类中再增加一个String字段页面:

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

点击时从Item中获取相应的页面值:

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

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多