ListView_Adpter
1.List.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"
>
<ImageView
android:id="@+id/img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/img3"
android:layout_centerInParent="true"
>
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/t1"
/>
</RelativeLayout>
</RelativeLayout>
2.Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFAF0"
>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下一个视图布局"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
4. TestadpterActivity:
/**
* 自定义适配器
*
* @author hongj
*
*/
public class TestadpterActivity extends Activity {
private ListView listview;
private Myadpter myadpter;
private Button button;
private List<Student> list = new ArrayList<Student>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Student student = new Student();
student.age="20";
student.name="张三";
student.sex="女";
list.add(student);
Student student2 = new Student();
student2.age="20";
student2.name="王五";
student2.sex="男";
list.add(student2);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TestadpterActivity.this,TestSimpleAdapter.class);
startActivity(intent);
}
});
listview = (ListView) findViewById(R.id.list);
myadpter = new Myadpter();
listview.setAdapter(myadpter);
}
private class Holder {
TextView hTextView1;
TextView hTextView2;
ImageView imageView3;
}
class Myadpter extends BaseAdapter {
private LayoutInflater inflater = null;
public Myadpter() {
super();
inflater = LayoutInflater.from(TestadpterActivity.this);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
if (null == convertView) {
convertView = inflater.inflate(R.layout.list, parent, false);
}
holder.hTextView1 = (TextView) convertView.findViewById(R.id.t1);
holder.hTextView2 = (TextView) convertView.findViewById(R.id.t2);
holder.imageView3 = (ImageView) convertView.findViewById(R.id.img3);
holder.hTextView1.setText(list.get(position).name);
holder.hTextView2.setText(list.get(position).sex);
holder.imageView3.setImageResource(R.drawable.icon);
return convertView;
}
}
class Student{
String name;
String sex;
String age;
}
}
5. TestSimpleAdapter:
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class TestSimpleAdapter extends Activity {
private ListView listView;
private Button button;
private SimpleAdapter simpleAdapter;
private ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.list);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TestSimpleAdapter.this,TestArrayAdapter.class);
startActivity(intent);
}
});
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("textview1", "textview1");
map.put("textview2", "TEXTVIEW2");
map.put("imag3", R.drawable.icon);
String[] from ={"textview1","textview2","imag3"};
int[] to ={R.id.t1,R.id.t2,R.id.img3};
list.add(map);
simpleAdapter = new SimpleAdapter(this, list, R.layout.list, from, to);
listView.setAdapter(simpleAdapter);
}
}
6. TestArrayAdapter:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class TestArrayAdapter extends Activity {
private ArrayAdapter arrayAdapter;
private ListView listView;
private List list;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = new ArrayList();
list.add("你好,这是ArrayAdapter");
listView = (ListView) findViewById(R.id.list);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,list);
listView.setAdapter(arrayAdapter);
button = (Button) findViewById(R.id.btn);
button.setVisibility(View.GONE);
}
}
7.效果图: