我想在点击事件中更新列表视图,但是因为我在oncreate()之外的函数中添加了项目,所以点击事件中的调用不会更新列表视图。
public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
List<String> itemlist = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing the array lists and the adapter
Button add = findViewById(R.id.add);
EditText editText = findViewById (R.id.editText);
ListView listView = (ListView) findViewById (R.id.listView);
Button delete = findViewById (R.id.delete);
Button clear = findViewById(R.id.clear);
ArrayAdapter adapter = new ArrayAdapter (MainActivity.this, android.
R.layout.simple_list_item_multiple_choice, itemlist);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TimePickerFragment timePicker = new TimePickerFragment();
timePicker.show(getSupportFragmentManager(),"time picker");
//If i add items like this, everyting works fine
// itemlist.add(editText.getText().toString());
// listView.setAdapter(adapter);
// adapter.notifyDataSetChanged();
// // This is because every time when you add the item the input space or the eidt text space will be cleared
// editText.getText().clear();
}
}
);
// Selecting and Deleting the items from the list when the delete button is pressed
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SparseBooleanArray position = listView.getCheckedItemPositions();
Integer count = listView.getCount();
int item = count - 1;
while (item >= 0) {
if (position.get(item)) {
itemlist.remove(item);
// adapter.remove(itemlist.get(item));
}
item--;
}
position.clear();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, itemlist);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
// Clearing all the items in the list when the clear button is pressed
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemlist.clear();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, itemlist);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
itemlist.add("The time selected is "+i+":"+i1);
ListView listView=findViewById(R.id.listView);
ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, itemlist);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}t.simple_list_item_multiple_choice, itemlist);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
itemlist.add("The time selected is "+i+":"+i1);
ListView listView=findViewById(R.id.listView);
/// TODO: Make adapter accessible from here, dont create a local one
ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, itemlist);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
个字符
当我在注解部分添加类似的项目时,我不必每次都初始化适配器。我可以使用相同的适配器,但当我在ontimeset中添加项目时,我必须初始化一个新的适配器(我尝试将适配器用作类成员,但不起作用),因此我认为,oncreate中的更新不会更改UI
1条答案
按热度按时间z3yyvxxp1#
您只需在
onCreate()
中定义一次适配器,然后从任何地方更新它,只需确保它可以使用全局变量访问。就像您的itemList
可以从任何地方访问一样,您可以从列表中添加/删除项目,并调用adapter.notifyDataSetChanged()
,这将根据您的列表自动更新列表视图和适配器。但是,请确保将适配器附加到onCreate()
本身的列表视图中。