重写BaseAdapter实现ListView

public class BaseAdapterActivity extends BaseActivity {
    private ListView base_adapter_listView;
    private ListViewFruit[] fruitList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_adapter);

        base_adapter_listView = (ListView) findViewById(R.id.base_adapter_listView);
        BaseAdapter baseAdapter = new BaseAdapter() {
            //重写该方法指定数量40
            @Override
            public int getCount() {
                return 40;
            }

            @Override
            public Object getItem(int position) {
                return 0;
            }

            //重写该方法获得id
            @Override
            public long getItemId(int position) {
                return position;
            }

            //重写getView() 方法,这个方法在内容被加载之前会执行
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100,100,0);
                LinearLayout linearLayout = new LinearLayout(BaseAdapterActivity.this);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                ImageView imageView = new ImageView(BaseAdapterActivity.this);
                TextView textView = new TextView(BaseAdapterActivity.this);
                imageView.setImageResource(R.drawable.qq_icon);
                textView.setText("第" + (position + 1) + "个列表");
                linearLayout.addView(imageView,layoutParams);
                linearLayout.addView(textView);
                return linearLayout;
            }
        };
        base_adapter_listView.setAdapter(baseAdapter);
重写四个方法,在getView中设置布局

转载于:https://www.cnblogs.com/cenzhongman/p/xiao.html

你可能感兴趣的:(重写BaseAdapter实现ListView)