Android入门之Adapter的简单使用教程

  package org.mk.android.demo.demobasicadapter;

  import androidx.appcompat.app.AppCompatActivity;

  import android.os.Bundle;

  import android.widget.ArrayAdapter;

  import android.widget.ListView;

  import java.util.ArrayList;

  import java.util.List;

  public class MainActivity extends AppCompatActivity {

  List titleList=new ArrayList();

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  //要显示的数据

  titleList.add("雷神");

  titleList.add("基神");

  titleList.add("基神");

  ArrayAdapter adapter = new ArrayAdapter

  (this,android.R.layout.simple_expandable_list_item_1,titleList);

  //获取ListView对象,通过调用setAdapter方法为ListView设置Adapter设置适配器

  ListView listView = (ListView) findViewById(R.id.listView);

  listView.setAdapter(adapter);

  }

  }