我有一个包含ListView的屏幕的
Android应用程序,我正在使用它来显示设备列表.这些设备被保存在一个阵列中.
我试图使用ArrayAdapter在列表中的屏幕上显示数组中的内容.
当我第一次加载SetupActivity类时,它可以工作,但是在addDevice()方法中有一个新的设备添加,这意味着更新保存设备的阵列.
我正在使用notifyDataSetChanged(),它应该更新列表,但似乎不起作用.
public class SetupActivity extends Activity
{
private ArrayList deviceList;
private ArrayAdapter 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();
deviceList = populateDeviceList(); //Get all the devices into the list
arrayAdapter = new ArrayAdapter(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
}
}
任何人都可以看到我出错的地方吗?