大佬教程收集整理的这篇文章主要介绍了java – 具有ArrayList和ListView的Android Array Adapter在更改阵列列表时不会更新,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用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 } }
任何人都可以看到我出错的地方吗?
>使用清除来清除适配器 – arrayAdapter.clear()
>使用Adapter.add并添加新形成的列表 – arrayAdapter.add(deviceList)
>调用notifyDataSetChanged
备择方案:
>新的设计师成立后重复这一步 – 但这是
冗
arrayAdapter = new ArrayAdapter<Device>(this,deviceList);
>创建您自己的类派生自BaseAdapter和listadapter给你更多的灵活性.这是最推荐的.
以上是大佬教程为你收集整理的java – 具有ArrayList和ListView的Android Array Adapter在更改阵列列表时不会更新全部内容,希望文章能够帮你解决java – 具有ArrayList和ListView的Android Array Adapter在更改阵列列表时不会更新所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。