设置日期时是否可以更改ListView项的文本?

时间:2015-05-10 21:12:22

标签: android

我有一个ListView显示选项选择日期,选择时间。我有一个内部类实现OnDateSetListener和OnTimeSetListener。

有没有办法在设定日期或时间后更新项目?

listViewOptions = (ListView) findViewById(R.id.listview_options);
String[] options = new String[]{"Choose date...", "Choose time..."};
    ArrayAdapter<String> optionsAdapter = new ArrayAdapter<>(
            getApplicationContext(),
            R.layout.simple_list_item,
            options
    );
    listViewOptions.setAdapter(optionsAdapter);
    listViewOptions.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Calendar date = Calendar.getInstance(Locale.US);
            switch (position) {
                case 0: {
                    //show date picker
                    DatePickerDialog datePicker = new DatePickerDialog(
                            context,
                            new MyListener(),
                            date.get(Calendar.YEAR),
                            date.get(Calendar.MONTH),
                            date.get(Calendar.DAY_OF_MONTH)
                    );
                    datePicker.getDatePicker().setMinDate(date.getTimeInMillis());
                    datePicker.show();
                    break;
                }
                case 1: {
                    TimePickerDialog timePicker = new TimePickerDialog(
                            context,
                            new MyListener(),
                            date.get(Calendar.HOUR_OF_DAY),
                            date.get(Calendar.MINUTE),
                            false
                    );
                    timePicker.show();
                    break;
                }
            }
        }
    });

内在阶级:

private class MyListener implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {


    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        //change text of the "choose date..." item
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        //change text of the "choose time..." item
    }
}

1 个答案:

答案 0 :(得分:0)

您需要一个自定义适配器和一个公共方法notifyDataSetChanged();并在其中,更改您的数据集,并在完成后,调用import pickle test_list = [{'Key' : 'key1', 'List' : [1,2,3]}, {'Key' : 'key2', 'List' : [4,5,6]}] pickle.dump(test_list,open('list.p','wb')) test_list = pickle.load(open('list.p','rb')) print test_list >>> [{'List': [1, 2, 3], 'Key': 'key1'}, {'List': [4, 5, 6], 'Key': 'key2'}] 以便ListView刷新新数据。

有关如何实现自定义列表视图适配器的详细说明和示例,请访问此网站