0

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 ) ;
}

}
Shudy
  • 7,806
  • 19
  • 63
  • 98
Moaaz
  • 35
  • 8
  • Ideally you would want to use a local database here, please do checkout 'Room' – because_im_batman Dec 08 '19 at 20:56
  • thank you for your comment , honestly now i just want to add item to an array list , Without storage it for now , so can you help me how pass data from "add" fragment to "alarm" fragment " , many thanks. – Moaaz Dec 08 '19 at 21:02

1 Answers1

0

You will need to do 2 things. The first one is the one you are directly answering, and the second one is the one you have already been suggested (storing them).

For storing, if you are doing something really simple, you could do so with SharedPreferences. So you store (alarm_1_time, "00:00) (alarm_1_name, "my name") for each alarm. Check this and this. Otherwise you can try more complex methods (maybe 'Room', which I have not tried out, but some fellow user is suggesting).

For adding to the list view, you need to:

  • Firstly, pass from one fragment to another one: as stated here. You basically need to do it through an activity.

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

Source here

Then, to do so:

step 1. Send the data from the new alarm

In add fragment:

Intent intent = new Intent(getActivity().getBaseContext(), YourActivity.class);  // activity that hosts the fragments
intent.putExtra("name", alarm_name);
// you can pass as many as you want (labels, names, times, colours, repeating, active, etc.)
// for example:
// intent.putExtra("time", alarm_time);
getActivity().startActivity(intent);

step 2. Receive the data from the new alarm in the activity

In the activity that hosts the fragments:

Intent intent = getIntent();
String my_alarm_name = intent.getStringExtra("name");

Step 3. now from activity, we send it to the fragment:

Bundle bundle = new Bundle();
bundle.putString("alarm", my_alarm_name);
// do the same for all other info (such as time, repetition, etc.) similarly
// now set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

Step 4. receive in fragment in Fragment onCreateView method:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
          String strtext=getArguments().getString("message");

    return inflater.inflate(R.layout.fragment, container, false);
    }

Here you have an example of this method with all the code. An alternative way is through interfaces, as stated here; here you have a question regarding this.

  • On second place, add new element to list view in the alarms fragment. Here you have a question with more information regarding this, and here a post with a more detailed explanation.

     names.add(new_alarm) // append the new alarm to the list you have
     adapter.notifyDataSetChanged(); //notify your custom adapter so that it "refreshes"
    
miquelvir
  • 1,748
  • 1
  • 7
  • 21