Tôi có một ứng dụng Android có màn hình bao gồm ListView, mà tôi đang sử dụng để hiển thị danh sách thiết bị. Các thiết bị này được giữ trong một mảng.Bộ chuyển đổi Array Android với ArrayList và ListView không cập nhật khi danh sách đã thay đổi
Tôi đang cố gắng sử dụng ArrayAdapter để hiển thị những gì nằm trong mảng trên màn hình trong danh sách.
Nó hoạt động khi tôi lần đầu tiên nạp lớp SetupActivity, tuy nhiên, đó là cơ sở để thêm một thiết bị mới trong AddDevice() phương pháp, có nghĩa là mảng giữ các thiết bị được cập nhật.
Tôi đang sử dụng thông báoDataSetChanged() cần phải cập nhật danh sách nhưng có vẻ như không hoạt động.
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
}
}
Có ai có thể thấy tôi đang đi đâu không?
bận tâm, đó chỉ là lỗi đánh máy trên phần của tôi ... xin lỗi vì sự nhầm lẫn –
hey pippa, nó thật tuyệt, không to tát. – petey
bạn có thể đăng createNewDeviceList của bạn(), rất có thể là nó không thêm bất cứ điều gì mới vào danh sách của bạn và tạo danh sách mới với cùng một yếu tố và thứ tự trong bản gốc – petey