您好,登錄后才能下訂單哦!
基于BaseAdapter 實現ListView
注意擴展BaseAdapter 需要重寫以下四個方法
getCount();
getItem();
getItemId();
getView();
程序清單
package xiaocool.net.listviewtest; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private ListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview2); list=(ListView)this.findViewById(R.id.list1); /*//定義一個數組 String arr[]={"校酷網絡1","校酷網絡2","校酷網絡3","校酷網絡4","校酷網絡5"}; //將數組包裝成ArrayAdapter ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.array_item,R.id.textView,arr); list.setAdapter(adapter);*/ BaseAdapter adapter=new BaseAdapter() { //返回總的列表項數 @Override public int getCount() { return 20; } @Override public Object getItem(int position) { return null; } //返回列表項的id @Override public long getItemId(int position) { return 0; } //返回每個具體item的樣子 @Override public View getView(int position, View convertView, ViewGroup parent) { //創建一個LinearLayout,并向其中添加兩個控件 LinearLayout layout=new LinearLayout(MainActivity.this); layout.setOrientation(0); ImageView p_w_picpathView=new ImageView(MainActivity.this); p_w_picpathView.setImageResource(R.drawable.syudio); TextView textView=new TextView(MainActivity.this); textView.setText("第"+(position+1)+"項"); textView.setTextColor(Color.BLUE); textView.setTextSize(20); layout.addView(p_w_picpathView); layout.addView(textView); return layout; } }; list.setAdapter(adapter); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。