当我点击"新项目"中的按钮时,我正在尝试保存一些信息。页。之后,我想将这些信息保存在" Projects"页面并显示在"我的项目"页。出于某种原因,它没有显示任何内容。请帮帮忙?
新项目页面:
public class NewProjectFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
LinearLayout container;
TextView new_project;
String tv_project_name;
private OnFragmentInteractionListener mListener;
public NewProjectFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment NewProjectFragment.
*/
// TODO: Rename and change types and number of parameters
public static NewProjectFragment newInstance(String param1, String param2) {
NewProjectFragment fragment = new NewProjectFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
Button addButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_new_project, container, false);
new_project=(TextView) view.findViewById(R.id.textView_new_project);
tv_project_name= (String) new_project.getText();
addButton= (Button) view.findViewById(R.id.button_add_project);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
addItem(tv_project_name);
}
});
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public void addItem(String name) {
Projects project=new Projects();
project.addItem(tv_project_name);
}
}
我的项目页面:
public class MyProjectsFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public MyProjectsFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment MyProjectsFragment.
*/
// TODO: Rename and change types and number of parameters
public static MyProjectsFragment newInstance(String param1, String param2) {
MyProjectsFragment fragment = new MyProjectsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
ListView listView;
ArrayList<String> listItems= Projects.getArrayList();
ArrayAdapter<String> adapter;
@Override
public void onStart() {
super.onStart();
adapter= new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
Create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_my_projects, container, false);
listView=(ListView) view.findViewById(R.id.listview_projects);
listItems= new ArrayList<String>();
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public void Create() {
/* listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
}
});*/
}
}
项目类:
public class Projects {
static ArrayList<String> names= new ArrayList<String>();
public void addItem(String name)
{
names.add(name);
}
public static ArrayList getArrayList()
{
return names;
}
}
答案 0 :(得分:0)
每次您输入My Project
时,您都会在listItems= new ArrayList<String>()
中onCreateView
。它没有明确的数据。
每当你addItem
时,你会初始化一个新的Project
,这很奇怪。
我的建议是:
public class Projects {
static ArrayList<String> names= new ArrayList<String>();
public static void addItem(String name)
{
names.add(name);
}
public static ArrayList getArrayList()
{
return names;
}
}
Project.getArrayList()
到getList
Project.addItem("")
添加数据
这种方式还不是很合适,但应该可行。