Android Programming -15 Custom Adapter

Hi guys in this tutorial we will continue the custom adapter.

In my previous article, we started to customize of the listview. First of all, I recommend you to read it. You can read it here.

package com.example.teka.icim_rahat.adapters;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.teka.icim_rahat.R;
import com.example.teka.icim_rahat.models.icirahat_class;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by teka on 25.7.2017.
 */

public class kullanici_adpter extends ArrayAdapter<icirahat_class> {

public kullanici_adpter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<icirahat_class> objects) throws IOException {

super(context, resource, objects);}

int sayac=0;

@NonNull
    @Override

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View v = convertView;

if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.icim_rahat_class, null);}

icirahat_class hs = getItem(position);

if (hs != null) {

final ImageView txt = (ImageView) v.findViewById(R.id.txt);
            TextView yorum = (TextView) v.findViewById(R.id.yorum);
            final Button like = (Button) v.findViewById(R.id.like);
           txt.setImageResource(R.drawable.res);
            yorum.setText("cocuklarrrr");
            like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sayac++;
                    if (sayac%2==1){
                    like.setBackgroundResource(R.drawable.like);}
                else {
                like.setBackgroundResource(R.drawable.like_white);}
                }

});}
return v;
    }
}

If we examine our code.

we extend our adapter class from Arrayadaper class. We create the type of ArrayAdapter from the model we created in the previous lesson. Then we define the construction method of the adapter. When defining this method, we determine what parameters we need to create during create.

We encode what we want to make our adapter into getView method.All the operations we will do on the components we add to the layout file we created for the views of the Listview item tags are also encoded for the adapter class.

For example, in our code, we change the text of textview and set an image in imageview with a drawable folder. Activate the click feature of the Like button, we change the background of the button according to the number of clicks.

About Kübra Hebeş

Bilgisayar Mühendisi

Leave a Reply

Your email address will not be published. Required fields are marked *