Android 从两个片段在列表视图中添加新项
我正在开发alarm应用程序,我是android开发的初学者 在我的应用程序中,我有3个选项卡“编辑”、“报警”和“添加报警”。我在“报警”选项卡中有列表视图,我想在“添加报警”选项卡中在该列表中添加新项目,并在报警中显示旧项目 这是我的密码 报警等级Android 从两个片段在列表视图中添加新项,android,android-fragments,Android,Android Fragments,我正在开发alarm应用程序,我是android开发的初学者 在我的应用程序中,我有3个选项卡“编辑”、“报警”和“添加报警”。我在“报警”选项卡中有列表视图,我想在“添加报警”选项卡中在该列表中添加新项目,并在报警中显示旧项目 这是我的密码 报警等级 public class Alarm extends Fragment { public ArrayList<Times> names = new ArrayList<>(); public Alarm() {
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,“我的名字”)。检查和。否则,您可以尝试更复杂的方法(可能是“房间”,我还没有尝试过,但一些用户建议) 要将添加到列表视图中,您需要:
- 首先,从一个片段传递到另一个片段:如前所述。您基本上需要通过活动来完成
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"