I am working on alarm app , and I am beginner in android development.
In my app I have 3 tabs "edit", "alarm" and "add alarm" .I have list view in "alarm" tab , and I want in "add alarm" tab add new item in that list , and show it in alarm with old items .
This is my code.
Alarm class
public class Alarm extends Fragment {
public ArrayList<Times> names = new ArrayList<>();
public Alarm() {
// Required empty public constructor
}
ListView list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_alarm, container, false);;
list = v.findViewById(R.id.listview);
//this demo items .
names.add(new Times( "03:32" , "AM" , "Mon,Wed" , "-Gym Time"));
names.add(new Times( "07:09" , "AM" , "Wed , Mon" , "-Home Time"));
names.add(new Times( "12:00" , "AM" , "Tuh" , "-Gym Time"));
names.add(new Times( "03:36" , "AM" , "Sun,Tue,Wed" , "-Gym Time"));
names.add(new Times( "05:32" , "AM" , "Wed , Mon" , "-Home Time"));
names.add(new Times( "03:52" , "AM" , "Mon" , "-Gym Time"));
names.add(new Times( "08:42" , "AM" , "Sun,Tue,Wed" , "-Gym Time"));
names.add(new Times( "10:22" , "AM" , "Wed , Mon" , "-Gym Time"));
myAdapter adapter = new myAdapter(getContext(), R.layout.custom_list_alarm , names);
Log.e("hi", "onCreateView: " + getContext() );
list.setAdapter(adapter);
return v;
}
}
and here myAdapter.java
public class myAdapter extends BaseAdapter {
private Context c;
Fragment fr ;
private int res ;
private ArrayList<Times> time;
public myAdapter() {
}
public myAdapter(Context c , int res , ArrayList<Times> time)
{
this.c = c;
this.res = res;
this.time = time;
}
public void addAlarm(Times times)
{
this.time.add(times);
}
@Override
public int getCount() {
return time.size();
}
@Override
public Times getItem(int position) {
return time.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null)
{
v = LayoutInflater.from(c).inflate(res , null , false);
}
TextView tv_name = v.findViewById(R.id.textView);
TextView tv_name2 = v.findViewById(R.id.textView2);
TextView tv_name3 = v.findViewById(R.id.textView3);
TextView tv_name4 = v.findViewById(R.id.textView4);
Times t = getItem(position);
tv_name.setText(t.getTime());
tv_name3.setText(t.getDay());
tv_name2.setText(t.getDays());
tv_name4.setText(t.getSubject());
return v;
}
}
Add Alarm class
public class Add extends Fragment {
public Add() {
// Required empty public constructor
}
private TimePicker timePicker ;
private TextView tv ;
private Button add ;
private EditText label;
private String format = "";
public String subject = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_add, container, false);
tv = (TextView) v.findViewById(R.id.textView5);
timePicker = (TimePicker) v.findViewById(R.id.datePicker1);
label = (EditText) v.findViewById(R.id.subject) ;
add = (Button) v.findViewById(R.id.addAlarm);
Log.e("hi2", "addNewAlarm: " + subject );
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNewAlarm();
}
});
return v;
}
public void addNewAlarm()
{
subject = label.getText().toString();
int hour = timePicker.getHour();
//int minute = timePicker.getCurrentMinute();
if (hour == 0) {
hour += 12;
format = "AM";
} else if (hour == 12) {
format = "PM";
} else if (hour > 12) {
hour -= 12;
format = "PM";
} else {
format = "AM";
}
Times alarm = new Times(String.valueOf(hour), String.valueOf(format), "Test", String.valueOf(subject));
myAdapter test = new myAdapter();
test.addAlarm(alarm);
//tv.setText(String.valueOf(hour + subject )+ subject ) ;
}
}