一.代码实现
1. “Activity_11\src\yan\activity_11\MainActivity.java”
package yan.activity_11; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { ListView listView; String [] titles={"标题1","标题2","标题3","标题4"}; String [] texts={"文本内容A","文本内容B","文本内容C","文本内容D"}; int [] resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("BaseAdapter for ListView"); listView=(ListView)this.findViewById(R.id.MyListView); listView.setAdapter(new ListViewAdapter(titles,texts,resIds)); } public class ListViewAdapter extends BaseAdapter{ View [] itemViews; public ListViewAdapter(String [] itemTitles, String [] itemTexts, int [] itemImageRes){ itemViews = new View[itemTitles.length]; for (int i=0; i
2.package yan.activity_11;
import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { ListView listView; String [] titles={"标题1","标题2","标题3","标题4"}; String [] texts={"文本内容A","文本内容B","文本内容C","文本内容D"}; int [] resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("BaseAdapter for ListView"); listView=(ListView)this.findViewById(R.id.MyListView); listView.setAdapter(new ListViewAdapter(titles,texts,resIds)); } public class ListViewAdapter extends BaseAdapter{ View [] itemViews; public ListViewAdapter(String [] itemTitles, String [] itemTexts, int [] itemImageRes){ itemViews = new View[itemTitles.length]; for (int i=0; i
二.“Activity_11\res\layout\main.xml”
<&#63;xml version="1.0" encoding="utf-8"&#63;><&#63;xml version="1.0" encoding="utf-8"&#63;>
三.“Activity_11\res\layout\listview_item.xml”
<&#63;xml version="1.0" encoding="utf-8"&#63;>
<&#63;xml version="1.0" encoding="utf-8"&#63;>