I want to update the list view inside click events but because I add the items inside a function that is outside the oncreate(), calls inside the click events dont update the listview.
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();
}
}
public class TimePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE) ;
return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute,android.text.format.DateFormat.is24HourFormat(getActivity()));
}
}
When i add items like in the commented part, i dont have to initialize adapter each time. I can use the same adapter but when i add items inside ontimeset i have to initialize a new adapter (i tried using adapter as a class member but didnt work) and because of this i think, the updates inside oncreate doesnt change the UI
You can just define your adapter once in your onCreate()
and update it from anywhere, just make sure it is accessible using a global variable. Just like your itemList
is accessible from anywhere, you can just add/remove items from your list, and call adapter.notifyDataSetChanged()
this will automatically update the list view and adapter according to your list. However make sure you attach your adapter to your list view in onCreate()
itself.