ListView--------------自定义适配器

ListView常用适配器,现在针对一些要求较高的ListView实现效果来时,自定义适配器,可以达到我们想要的结果。此次,我们自己重新书写Adapter,

来让我们的程序更加美观和实用性更强。

适配器如下:



import java.util.HashMap;
import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class ListAdapter extends BaseAdapter
{


private Context context ;
private LayoutInflater layoutInflater = null;
private List<HashMap<String, String>>data;

public ListAdapter(Context context,List<HashMap<String, String>>data){
this.context = context;
this.data = data;
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount()
{

return data.size();
}


@Override
public Object getItem(int arg0)
{

return data.get(arg0);
}


@Override
public long getItemId(int position)
{

return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent)
{
HolderView holderView = null;

 if (convertView == null)//我们再次是为了如果convertView实例化了,不用再实例化,可以节省内存
{
 holderView= new HolderView();
 convertView = layoutInflater.inflate(R.layout.item, null);
 holderView.numView = (TextView)convertView.findViewById(R.id.bianhao);
 holderView.nameView = (TextView)convertView.findViewById(R.id.name);
 holderView.ageView = (TextView)convertView.findViewById(R.id.age);
 
 convertView.setTag(holderView);
}else {
holderView = (HolderView)convertView.getTag();
}
 holderView.numView.setText(data.get(position).get("bianhao"));
 holderView.numView.setText(data.get(position).get("name"));
 holderView.numView.setText(data.get(position).get("age"));
return convertView;
}


private class HolderView{
TextView numView;
TextView nameView;
TextView ageView;
}
}


代码如下:


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


public class ListViewDemoActivity extends Activity
{
/** Called when the activity is first created. */

private ListView listView ;
private ListAdapter  adapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

listView = (ListView)findViewById(R.id.listView);
  

adapter = new  ListAdapter (ListViewDemoActivity.this,getData() );
listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener()
{

/*
* parant 表示的是哪一行listView
* view 是你的子布局 RelativeLayout
* position和id是相等的
*/
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{

ListView lsListView = (ListView)parent;
HashMap<String, String> hashMap = (HashMap<String, String>) parent.getItemAtPosition(position);

Toast.makeText(ListViewDemoActivity.this, hashMap.get("bianhao")+ hashMap.get("name"), Toast.LENGTH_SHORT).show();

}
});
}


private List<HashMap<String, String>> getData()
{


List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("bianhao", "编号");
map.put("name", "姓名");
map.put("age", "年龄");
data.add(map);
map = new HashMap<String, String>();
map.put("bianhao", "123");
map.put("name", "小张");
map.put("age", "123");
data.add(map);
map = new HashMap<String, String>();
map.put("bianhao", "123");
map.put("name", "小张");
map.put("age", "123");
data.add(map);
map = new HashMap<String, String>();
map.put("bianhao", "123");
map.put("name", "小张");
map.put("age", "123");
data.add(map);

return data;
}
}


//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#000000"></ListView>

</LinearLayout>





//item.xml

<?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="fill_parent" >


<TextView
android:id="@+id/bianhao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="15dip"
android:text="@string/hello" />


<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/age"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/bianhao"
android:padding="15dip"
android:text="@string/hello" />






<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dip"
android:layout_alignParentRight="true"
android:text="@string/hello" />


</RelativeLayout>

 

你可能感兴趣的:(ListView--------------自定义适配器)