Search code examples
javaandroidandroid-studioandroid-recyclerviewandroid-viewholder

Change color of selected item in recyclerview (Java)


I am working on this app for a school project, and it's the first time I am using java to develop an app.

I created a recyclerview in order to show multiple orders from a list. What I wanted it to do is, when it has been clicked, then change the color of the "nameText" to green.

My code is currently crashing my app everytime I select one of the items.

The main view of the recylerView:

public class Menu extends AppCompatActivity implements menuAdpter.OnNoteListener {

    RecyclerView recView;
    ArrayList<MenuItem> items = new ArrayList<MenuItem>();
    menuAdpter myAdapter;

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

        recView = findViewById(R.id.recycleView);
        getMenuItem();
        myAdapter = new menuAdpter(items, this);
        recView.setAdapter(myAdapter);

        recView.setLayoutManager(new LinearLayoutManager(this));


    }

// Getting information from the server:
    public void getMenuItem() {


        try {

            Request getMenuRequest = new Request(FISConstants.GET_MENU);

            String resultFromServerGetMenu = SimpleClient.makeRequest(FISConstants.HOST, getMenuRequest);
//            System.out.println(resultFromServerGetMenu);

            String[] returnedListOfWords = resultFromServerGetMenu.split("-|\\,");
            System.out.println(Arrays.toString(returnedListOfWords));

            for (int i = 0; i < returnedListOfWords.length - 3; i = i + 4) {
                MenuItem temp = new MenuItem(returnedListOfWords[i], returnedListOfWords[i + 1], returnedListOfWords[i + 2], returnedListOfWords[i + 3]);
                items.add(temp);
            }
            System.out.println(items);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error with server connection");
        }
    }

    @Override
    public void onNoteClick(int position) {
        System.out.println("Clicked!" + items.get(position).getName());
        menuAdpter.changeText();
    }
}

Code from viewAdpter:

public class menuAdpter extends RecyclerView.Adapter<menuViewHolder>{

    public static ArrayList<MenuItem> mData;
    private OnNoteListener mOnNoteListener;
    public static menuViewHolder holder;

    public menuAdpter(ArrayList<MenuItem> data, OnNoteListener onNoteListener) {
        mData = data;
        mOnNoteListener = onNoteListener;
    }

    public static ArrayList<MenuItem> getmData() {
        return mData;
    }

    @NonNull
    @Override
    public menuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View myView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items_menu, parent, false);
        holder = new menuViewHolder(myView, mOnNoteListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull menuViewHolder holder, int position) {
        // setting each value from the list to each field:
        holder.nameText.setText(mData.get(position).getName());
        holder.priceText.setText("$" + mData.get(position).getPrice());
        holder.descriptionText.setText(mData.get(position).getDescription());
        holder.typeText.setText(mData.get(position).getType());
        holder.amountText.setText("10");
    }

    public static void changeText() {
        System.out.println(holder);
        holder.nameText.setTextColor(0xff00ff00);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public interface OnNoteListener {
        void onNoteClick(int position);
    }
}

code from viewHolder:

public class menuViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    protected TextView nameText;
    protected TextView priceText;
    protected TextView descriptionText;
    protected TextView amountText;
    protected TextView typeText;

    public static ArrayList<MenuItem> mData;

    menuAdpter.OnNoteListener onNoteListener;

    public menuViewHolder(@NonNull View itemView, menuAdpter.OnNoteListener onNoteListener) {
        super(itemView);

        nameText = itemView.findViewById(R.id.nameTextView);
        priceText = itemView.findViewById(R.id.priceTextView);
        descriptionText = itemView.findViewById(R.id.descriptionTextView);
        amountText = itemView.findViewById(R.id.amountTextView);
        typeText = itemView.findViewById(R.id.typeTextView);

        this.onNoteListener = onNoteListener;

        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        onNoteListener.onNoteClick(getAdapterPosition());
    }
}

What I was hoping the code to do is onNoteClick to call the function menuAdpter.changeText(); and change the color of the selected item.

I would really appreciate if someone could tell me how I can change the color of the selected item.

THank you!


Solution

  • You should update the textView color in the onclick(), try this:

      @Override
        public void onClick(View view) {
             textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));
        }