简单的ArrayAdapter

最后更新于:2022-04-01 14:43:05

如果只是单纯显示文字的话,ArrayAdapter是一个不错的选择。不用像之前的BaseAdapter那样写这么多东西。不过我还是习惯用BaseAdapter啊 ~~~ package com.example.mylistview; import java.util.Random; import java.util.Vector; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView lv; private Random rd; private Vector arr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); rd = new Random(); arr = new Vector(); for ( int i = 0 ; i < 50 ; i ++ ) { arr.add(String.valueOf(rd.nextInt(5000))); } lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.item_text,arr)); } } ~~~
';