I am writing the first application. At one of the stages a problem arose: there are two consecutive Activities that need to be linked. In the first, the Grid View, to which ImageAdaper is connected:
public class ImageAdapter extends BaseAdapter { private Context mContext; //Добавляем изображения в массив: public Integer[] mThumbIds = { R.drawable.1,R.drawable.2, R.drawable.3,R.drawable.4, R.drawable.5,R.drawable.6, }; //Конструктор: public ImageAdapter(Context c){ mContext = c; } @Override public int getCount() { return mThumbIds.length; } @Override public Object getItem(int position) { return mThumbIds[position]; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(mThumbIds[position]); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new GridView.LayoutParams(250, 300)); return imageView; } Activity itself receives info from the adapter, and displays 6 images in the GridView-image view:
public class Pick_type extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pick_type); GridView Grid = (GridView) findViewById(R.id.activity_pick_type); Grid.setAdapter(new ImageAdapter(this)); } At this stage, everything is fine.
Further, the task when clicking on the corresponding image — one of 6 imageview — to open in the new activity2 imageview only one, but strictly specified (for example, the R.drawable.1 view in the first activity- corresponds to the R.drawable.11 view in the second activity, R. drawable.2 - corresponding to R.drawable.22, etc.).
In search of possible options, I found the following answer: launching activity with parameters .
When trying to do this, it stalled. Made in the second activity a view and intent-successor. But connecting them to the first activity does not work.
Second activity
ImageView imagetake = (ImageView) findViewById(R.id.imageView2); Intent intent = getIntent(); if (intent.getIntExtra("image1",1) == 1){ imagetake.setImageResource(R.drawable.11); 1) The variable mThumbIds - cannot be applied to static content, according to android studio. Appropriately, the design as in the example.
public void onClick(ImageAdapter(this)) { Intent intent; switch (ImageAdapter.mThumbIds.getId()) { case R.drawable.top: intent = new Intent(MainActivity.this,activity2.class); intent.putExtra("image1", 1); startActivity(intent); break; It does not work for me.
2) In the above question, the elements are in .xml and they are assigned the onClick method and id. How to assign it to my views, I can not understand yet (and in general, maybe there is another way).
Tell me who can, what are the options to perform my task, or help sort out the errors.
onClick(...)Called? Where it is located? - post_zeew