Mehmet Ethem SULAN :: Android :: Android Custom ListView Items and Adapter

Android ile Custom ListView oluşturabilmek için Adapter yazmamız gerekiyor. ListActivity ait create metoudunda this.setListAdapter(new MyCustomAdapter(this, liste)); şeklinde adapter ekliyoruz. Sonra istediğimiz şekilde adapterimizi düzenliyoruz. Dikkat edilmesi gereken şey ise adapter.xml de hangi component tanimlanmışsa private AdaptirSatir{} sinifinda ayni bileşenler tanimlanmalidir ve MyCustomerAdapter içindeki getView() metodundan findByIde() metodu ile erişmemiz lazım.

custom listview

MyListActivity.java

package com.custom.list;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class MyListActivity extends ListActivity {
	private String[] liste = { "PYTHON", "C", "C", "C++", "C#", "MATLAB","PHP", "JAVA", "JSP", "JSF", "JPA", "SEAM", "ANDROID" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setListAdapter(new MyCustomAdapter(this, liste));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Object clicked = this.getListAdapter().getItem(position);
		String o = clicked.toString();
		Toast.makeText(getApplicationContext(), o, Toast.LENGTH_SHORT).show();
	}

}

MyCustomAdapter.java

package com.custom.list;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MyCustomAdapter extends ArrayAdapter<String>{

	private Activity context;
	private String[] languages;
	private LayoutInflater layoutInflater;

	private AdapterSatir adaSatir;

	public MyCustomAdapter(Activity context, String[] objects) {
		super(context, R.layout.adabter, objects);
		this.context=context;
		this.languages=objects;

	}

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

	View view_satir=convertView;
		if(view_satir==null)
		{
			adaSatir=new AdapterSatir();
			layoutInflater=context.getLayoutInflater();
			view_satir=layoutInflater.inflate(R.layout.adabter, null,true);
			adaSatir.textView=(TextView) view_satir.findViewById(R.id.textView1_adapter_tx);
			adaSatir.image_cut=(ImageView) view_satir.findViewById(R.id.imageView1_cut);
			adaSatir.image_option=(ImageView) view_satir.findViewById(R.id.imageView1_option);
			view_satir.setTag(adaSatir);
		}else{
			adaSatir=(AdapterSatir) view_satir.getTag();
		}
		adaSatir.image_cut.setImageResource(R.drawable.cut);
		adaSatir.image_option.setImageResource(R.drawable.options);
		adaSatir.textView.setText(languages[position]);

		adaSatir.image_option.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("Clicked", "Option tiklandi");

			}
		});
		adaSatir.image_cut.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("Clicked", "Cut tiklandi");

			}
		});
		return view_satir;

	}

	//adapter.xml icindeki ne tanimlanmisssa bu private icinde aynisini taimliyorum
	private class AdapterSatir
	{
		public TextView textView;
		public ImageView image_cut;
		public ImageView image_option;
	}
}

adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="horizontal">
    <TextView android:text="TextView" android:id="@+id/textView1_adapter_tx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
    <ImageView android:src="@drawable/icon" android:layout_height="wrap_content" android:id="@+id/imageView1_option" android:layout_width="wrap_content" android:layout_weight="1"></ImageView>
    <ImageView android:src="@drawable/icon" android:layout_height="wrap_content" android:id="@+id/imageView1_cut" android:layout_width="wrap_content" android:layout_gravity="right" android:layout_weight="1"></ImageView>
</LinearLayout>

Download CustomListAdapter.tar.gz

Comments

There are 3 comments for this post.

  1. Mehmet Ethem SULAN :: Android :: Android Create Customize Adapter and ListView Items on Eylül 9, 2011 5:54 pm

    […] oradan alınıyor ama örnek sade olsun diye o tarafa girmedim. Bu örneği daha iyi anlamak için Android Custom ListView Items and Adaptera […]

  2. Önder on Eylül 28, 2012 11:29 am

    Bu örnekte log a yazdırıyoruz ama listitemin hangi pozisyonundaki option yada cut a tıklandığı bilgisini çağırmayı denedim ama başaramadım onclick eventenda hangi kodu yazmam gerekir….. İlgin için teşekkürler şimdiden

  3. mustafa on Aralık 10, 2012 3:50 am

    Örnek bu kardesim;
    public static i=0;
    TextView icindekiler=(TextView)findViewById(R.id.textView1);
    icindekiler.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    Intent i=new Intent(Main.this,deneme.class);
    startActivity(i);
    }
    });
    }

Write a Comment

Let me know what you think?