我有一个 android 应用程序,其屏幕由一个 ListView 组成,我用它来显示设备列表。这些设备被保存在一个阵列中。
我正在尝试使用 ArrayAdapter 在列表中的屏幕上显示数组中的内容。
它在我第一次加载SetupActivity类时工作,但是,可以在addDevice()方法中添加新设备,这意味着保存设备的数组已更新。
我正在使用notifyDataSetChanged()应该更新列表,但它似乎不起作用。
public class SetupActivity extends Activity
{
private ArrayList<Device> deviceList;
private ArrayAdapter<Device> arrayAdapter;
private ListView listView;
private DevicesAdapter devicesAdapter;
private Context context;
public void onCreate(Bundle savedInstanceState) //Method run when the activity is created
{
super.onCreate(savedInstanceState);
setContentView(R.layout.setup); //Set the layout
context = getApplicationContext(); //Get the screen
listView = (ListView)findViewById(R.id.listView);
deviceList = new ArrayList<Device>();
deviceList = populateDeviceList(); //Get all the devices into the list
arrayAdapter = new ArrayAdapter<Device>(this, android.R.layout.simple_list_item_1, deviceList);
listView.setAdapter(arrayAdapter);
}
protected void addDevice() //Add device Method (Simplified)
{
deviceList = createNewDeviceList(); //Add device to the list and returns an updated list
arrayAdapter.notifyDataSetChanged(); //Update the list
}
}
谁能看到我哪里出错了?