参考资料:http://www.runoob.com/w3cnote/android-tutorial-listview.html
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.expample.myapplication.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/mainView_id"/> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:id="@+id/subView_id"/> <TextView android:layout_width="match_parent" android:layout_height="5dp" android:background="#000000"/> </LinearLayout>
public class MainActivity extends Activity { ListView lv; List ls; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView)findViewById(R.id.listView_id); ls = new LinkedList<animals>(); ls.add(new animals("猪说", "我是猪吗?")); ls.add(new animals("猫说", "我是猫吗?")); ls.add(new animals("狗说", "我是狗吗?")); ls.add(new animals("熊说", "我是熊吗?")); ls.add(new animals("兔说", "我是兔吗?")); ls.add(new animals("虎说", "我是虎吗?")); ls.add(new animals("鼠说", "我是鼠吗?")); ls.add(new animals("鸡说", "我是鸡吗?")); ls.add(new animals("鱼说", "我是鱼吗?")); MyAdapter adapter = new MyAdapter((LinkedList<animals>)ls, MainActivity.this); lv.setAdapter(adapter); } class animals{ String main; String sub; animals(String main, String sub){ this.main = main; this.sub = sub; } String getMain(){ return main; } String getSub(){ return sub; } } class MyAdapter extends BaseAdapter{ LinkedList<animals> mdata; Context mcontext; MyAdapter(LinkedList<animals> data, Context context){ this.mdata = data; this.mcontext = context; } @Override public int getCount() { return mdata.size(); } @Override public Object getItem(int position) { return mdata.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView main; TextView sub; LayoutInflater layoutInflater = LayoutInflater.from(mcontext); convertView = layoutInflater.inflate(R.layout.item_list_animal, null, false); main = (TextView) convertView.findViewById(R.id.mainView_id); sub = (TextView)convertView.findViewById(R.id.subView_id); main.setText(mdata.get(position).getMain()); sub.setText(mdata.get(position).getSub()); return convertView; } } }