AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tr.com.emrahkahraman.customlistview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity sınıfı
package tr.com.emrahkahraman.customlistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
ArrayList<Model> liste=new ArrayList<Model>();
ListView listemiz=null;
Model model=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listemiz=(ListView) findViewById(R.id.listView1);
getList();
listemiz.setAdapter(new ModelArrayAdapter(getApplicationContext(), liste));
listemiz.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Toast.makeText(MainActivity.this, "Listeye tıklandı", Toast.LENGTH_LONG).show();
}
});
}
public void getList()
{
Model m1= new Model();
m1.setAd("Emrah");
m1.setSoyad("Kahraman");
m1.setTcNo("12345678911");
m1.setDogumTarihi("01/01/1984");
Model m2= new Model();
m2.setAd("Hakan");
m2.setSoyad("Mert");
m2.setTcNo("12345678912");
m2.setDogumTarihi("01/01/1983");
liste.add(m1);
liste.add(m2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
Model sınıfı
package tr.com.emrahkahraman.customlistview;
import java.io.Serializable;
public class Model implements Serializable
{
private String ad;
private String soyad;
private String tcNo;
private String dogumTarihi;
public Model()
{
}
public String getAd() {
return ad;
}
public void setAd(String ad) {
this.ad = ad;
}
public String getSoyad() {
return soyad;
}
public void setSoyad(String soyad) {
this.soyad = soyad;
}
public String getTcNo() {
return tcNo;
}
public void setTcNo(String tcNo) {
this.tcNo = tcNo;
}
public String getDogumTarihi() {
return dogumTarihi;
}
public void setDogumTarihi(String dogumTarihi) {
this.dogumTarihi = dogumTarihi;
}
}
ModelArrayAdapter sınıfı
package tr.com.emrahkahraman.customlistview;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ModelArrayAdapter extends ArrayAdapter<model>
{
private final Context context;
private List</model><model> values;
public ModelArrayAdapter(Context context, List</model><model> tvalues)
{
super(context, R.layout.list_item, tvalues);
this.context = context;
this.values = tvalues;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent,
false);
TextView tv_Ad = (TextView) rowView.findViewById(R.id.tv_Ad);
TextView tv_Soyad = (TextView) rowView.findViewById(R.id.tv_Soyad);
TextView tv_tcKimlik = (TextView) rowView.findViewById(R.id.tv_tcKimlik);
TextView tv_Dogum = (TextView) rowView.findViewById(R.id.tv_Dogum);
Model model = ((Model) values.get(position));
tv_Ad.setText(model.getAd());
tv_Soyad.setText(model.getSoyad());
tv_tcKimlik.setText(model.getTcNo());
tv_Dogum.setText(model.getDogumTarihi());
return rowView;
}
}
list_item.xml dosyası
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/rlt_main">
<LinearLayout android:id="@+id/LinearLayout01" android:paddingTop="10dip" android:orientation="horizontal" android:weightSum="4" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/tv_Ad"
android:text="" android:layout_weight="2" android:gravity="left"
android:textColor="#000000" android:textSize="15dp" android:textStyle="bold"></TextView>
<TextView android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/tv_tcKimlik"
android:text="" android:layout_weight="2" android:gravity="right"
android:textColor="#000000" android:textSize="15dp" ></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02" android:paddingBottom="10dip" android:layout_below="@id/LinearLayout01" android:orientation="horizontal" android:weightSum="4" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_width="0dp"
android:layout_height="wrap_content" android:text=""
android:id="@+id/tv_Soyad" android:layout_weight="2" android:gravity="left"
android:textColor="#000000" android:textSize="15dp" android:textStyle="bold"></TextView>
<TextView android:layout_width="0dp"
android:layout_height="wrap_content" android:text=""
android:id="@+id/tv_Dogum" android:layout_weight="2" android:gravity="right"
android:textColor="#000000" android:textSize="15dp" ></TextView>
</LinearLayout>
</RelativeLayout>
activity_main.xml dosyası
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="tr.com.emrahkahraman.customlistview.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtBaslik"
android:textStyle="bold"
android:paddingBottom="16dp"
android:gravity="center"
android:text="Listview Kullanımı"
/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/txtBaslik"
android:scrollbarStyle="outsideInset">
</ListView>
</RelativeLayout>