1. 程式人生 > >android適配器+listview

android適配器+listview

reat etc state public istview span 貴陽 str 簡單

簡單 繼承BaseAdapter:

技術分享圖片

Myadapter代碼:

public class Myadapter extends BaseAdapter {
    private Context context;
    private String[] ss;
   /* private List<User>list;*/
    public Myadapter(Context context,String[] ss){
        super();
        this.context=context;
        this.ss=ss;
    }
    @Override
    
//得到個數 public int getCount() { return ss.length; } @Override //得到所以索引,即i值 public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { String str
=ss[position]; TextView textView=new TextView(context); textView.setText(str); return textView; } }

MianActivity:

public class MainActivity extends AppCompatActivity {
   private String[] ss=new String[]{
            "山西","太原","山東","貴陽","天津","北京","洪湖"
    };
    @Override
    
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView = (ListView) findViewById(R.id.list_view); Myadapter adapter=new Myadapter(this,ss); // ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ss); listView.setAdapter(adapter); } }

進階baseAdatapter:

android適配器+listview