androidgridviewandroid-recyclerviewphotoswipe

How can pass intent position


Hey guys im trying to create a IMAGE gallery, but im having trouble in the intent,after the user will clicked the image its show toast message but i want to pass intent with image position, how can i do that?

also i want to create swipe the images in new activity

please help guys...thanks in advance..

this is the code:

public class IndianActivity extends AppCompatActivity {

    GridView gridView;

    String[] numberword = { "One","Two","Three","Four","Five","Six","Seven","Eight"};

    int[] numberImage = {R.drawable.testing,R.drawable.testing
            ,R.drawable.testing,R.drawable.testing,R.drawable.testing
            ,R.drawable.testing,R.drawable.testing,R.drawable.testing};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indian);

        gridView = findViewById(R.id.grid_view);

        MainAdapter adapter = new MainAdapter(IndianActivity.this,numberword,numberImage);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),"You Clicked "+numberword[+i],Toast.LENGTH_SHORT).show();
            }
        });


    }
}

Also here is my MainAdapter code :

public class MainAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private String[] numberWord;
    private int[] numberImage;
    private View convertView;

    public MainAdapter(Context c,String[] numberWord,int[] numberImage){
        context = c;
        this.numberWord = numberWord;
        this.numberImage = numberImage;


    }
    @Override
    public int getCount() {
        return numberWord.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {

        if (inflater == null){
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row_item, null);
        }

        ImageView imageView = convertView.findViewById(R.id.image_view);
       // TextView textView = convertView.findViewById(R.id.text_view);

        imageView.setImageResource(numberImage[i]);
        //textView.setText(numberWord[i]);
        return convertView;
    }
} 

Solution

  • Use this to "put" the file on you click listener

    Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
    String strName = null;
    int I=0
    i.putExtra("STRING_I_NEED", strName);
    i.putExtra("Item_pos", I);
    

    Then, to retrieve the value try something like:

    String newString,pos;
    if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
        pos=extras.getString("Item_pos")
    }
    } else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
    }