Home > OS >  How to implement onClick event individual child on gridview
How to implement onClick event individual child on gridview

Time:12-25

Firstly, I have seen a bunch of resources regarding this, but none of them helpful because they are using viewholder. But I'm using only adapter class. That's for, I want to know how to implement onClick event on the adapter class.

MainActivity.java

package com.sakibxhossain.gridlayout_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.GridView;

import com.sakibxhossain.gridlayout_practice.helper.Main_Adapter;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    int[] itemsImage = new int[] {
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo
    };

    String[] itemTitle = new String[]{
            "Sorting", "Searching",
            "Stack & Queue", "Tree",
            "Graph", "Dynamic",
            "Greedy", "String",
            "Hashing"
    };

    String[] itemDifficulty = new String[]{
            "Easy",
            "Easy",
            "Easy",
            "Medium",
            "Hard",
            "Hard",
            "Hard",
            "Hard",
            "Medium"
    };

    GridView gridView;
    Main_Adapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridView = findViewById(R.id.grid_view);

        adapter = new Main_Adapter(this, itemsImage, itemTitle, itemDifficulty);
        gridView.setAdapter(adapter);
    }
}

Main_Adapter.Java

package com.sakibxhossain.gridlayout_practice.helper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import com.sakibxhossain.gridlayout_practice.R;

import java.util.Objects;

public class Main_Adapter extends BaseAdapter {
    Context context;
    int[] item_image;
    String[] item_Title;
    String[] item_Difficulty;

    public Main_Adapter(Context context, int[] item_image, String[] item_Title, String[] item_Difficulty) {
        this.context = context;
        this.item_image = item_image;
        this.item_Title = item_Title;
        this.item_Difficulty = item_Difficulty;
    }

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

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

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

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

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

        ImageView item_image_assign = view.findViewById(R.id.item_image);
        TextView item_Title_assign = view.findViewById(R.id.item_name);
        TextView item_difficulty_assign = view.findViewById(R.id.item_difficulty_name);

        item_image_assign.setImageResource(item_image[position]);
        item_Title_assign.setText(item_Title[position]);
        item_difficulty_assign.setText(item_Difficulty[position]);

        if (Objects.equals(item_Difficulty[position], "Hard")) {
            item_difficulty_assign.setTextColor(Color.parseColor("#E91E63"));
        }else if(Objects.equals(item_Difficulty[position], "Medium")){
            item_difficulty_assign.setTextColor(Color.parseColor("#AA00FF"));
        }

        return view;
    }
}

Is it possible to do this.

What I try: I try to implement onClick event each individual child on gridview using an adapter class. But every one explaining with viewholder.

What I expect: I want to know if it is possible to implement the onClick event on the adapter class. If possible, then how to do this?

CodePudding user response:

You need to use Interface to achieve your needs :

1 - Create an Interface e.g. ClickListener :

public interface ClickListener {
    void onItemClicked(int position);
}

2 - Add it to your Adapter :

public class Main_Adapter extends BaseAdapter {
    Context context;
    int[] item_image;
    String[] item_Title;
    String[] item_Difficulty;
    private ClickListener clickListener;

    public Main_Adapter(Context context, int[] item_image, String[] item_Title, String[] item_Difficulty, ClickListener clickListener) {
        this.context = context;
        this.item_image = item_image;
        this.item_Title = item_Title;
        this.item_Difficulty = item_Difficulty;
        this.clickListener = clickListener;
    }

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

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

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

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
    
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.item,null);
    }

    ImageView item_image_assign = view.findViewById(R.id.item_image);
    TextView item_Title_assign = view.findViewById(R.id.item_name);
    TextView item_difficulty_assign = view.findViewById(R.id.item_difficulty_name);
    CardView cardView = view.findViewById(R.id.cardView);
    item_image_assign.setImageResource(item_image[position]);
    item_Title_assign.setText(item_Title[position]);
    item_difficulty_assign.setText(item_Difficulty[position]);

    if (Objects.equals(item_Difficulty[position], "Hard")) {
        item_difficulty_assign.setTextColor(Color.parseColor("#E91E63"));
    }else if(Objects.equals(item_Difficulty[position], "Medium")){
        item_difficulty_assign.setTextColor(Color.parseColor("#AA00FF"));
    }
    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clickListener.onItemClicked(position);
        }
    });
}

3 - Now call it from your MainActivity :

public class MainActivity extends AppCompatActivity {

    int[] itemsImage = new int[] {
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo, R.drawable.logo,
            R.drawable.logo
    };

    String[] itemTitle = new String[]{
            "Sorting", "Searching",
            "Stack & Queue", "Tree",
            "Graph", "Dynamic",
            "Greedy", "String",
            "Hashing"
    };

    String[] itemDifficulty = new String[]{
            "Easy",
            "Easy",
            "Easy",
            "Medium",
            "Hard",
            "Hard",
            "Hard",
            "Hard",
            "Medium"
    };

    GridView gridView;
    Main_Adapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gridView = findViewById(R.id.grid_view);

        adapter = new Main_Adapter(this, itemsImage, itemTitle, itemDifficulty, new ClickListener() {
            @Override
            public void onItemClicked(int position) {
                Toast.makeText(MainActivity.this, "Item Clicked : " position, Toast.LENGTH_SHORT).show();
            }
        });
        gridView.setAdapter(adapter);
    }
}
  • Related