Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从两个片段在列表视图中添加新项_Android_Android Fragments - Fatal编程技术网

Android 从两个片段在列表视图中添加新项

Android 从两个片段在列表视图中添加新项,android,android-fragments,Android,Android Fragments,我正在开发alarm应用程序,我是android开发的初学者 在我的应用程序中,我有3个选项卡“编辑”、“报警”和“添加报警”。我在“报警”选项卡中有列表视图,我想在“添加报警”选项卡中在该列表中添加新项目,并在报警中显示旧项目 这是我的密码 报警等级 public class Alarm extends Fragment { public ArrayList<Times> names = new ArrayList<>(); public Alarm() {

我正在开发alarm应用程序,我是android开发的初学者

在我的应用程序中,我有3个选项卡“编辑”、“报警”和“添加报警”。我在“报警”选项卡中有列表视图,我想在“添加报警”选项卡中在该列表中添加新项目,并在报警中显示旧项目

这是我的密码

报警等级

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

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

}

你需要做两件事。第一个是您直接回答的问题,第二个是您已经得到建议的问题(存储它们)

对于存储,如果您正在做一些非常简单的事情,您可以使用SharedReference来完成。因此,您可以为每个警报存储(alarm_1_time,“00:00”)(alarm_1_name,“我的名字”)。检查和。否则,您可以尝试更复杂的方法(可能是“房间”,我还没有尝试过,但一些用户建议)

要将添加到列表视图中,您需要:

  • 首先,从一个片段传递到另一个片段:如前所述。您基本上需要通过活动来完成
通常,您希望一个片段与另一个片段通信,例如 基于用户事件更改内容的示例。全部 片段到片段的通信是通过相关的 两个片段不应该直接通信

然后,为此:

步骤1。从新报警发送数据

在添加片段中:

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);
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);
步骤2。从活动中的新报警接收数据

在承载片段的活动中:

Intent intent = getIntent();
String my_alarm_name = intent.getStringExtra("name");
步骤3。现在从活动开始,我们将其发送到片段:

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);
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);
步骤4。在fragment onCreateView方法中的fragment中接收:

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

    return inflater.inflate(R.layout.fragment, container, false);
    }
您有一个包含所有代码的此方法的示例。另一种方法是通过接口,如前所述;您对此有疑问

  • 在第二位,将新元素添加到报警片段中的列表视图中。您有一个关于这方面的问题和一篇更详细的解释

     names.add(new_alarm) // append the new alarm to the list you have
     adapter.notifyDataSetChanged(); //notify your custom adapter so that it "refreshes"
    

理想情况下,您希望在此处使用本地数据库,请签出“房间”谢谢您的评论,老实说,现在我只想将项目添加到数组列表中,暂时不存储它,因此您能否帮助我如何将数据从“添加”片段传递到“报警”片段,非常感谢。