2017-03-15 163 views
0

我想節省一些信息時,我從「新項目」頁上點擊一個按鈕。之後,我想將這些信息保存在「項目」頁面中,並將其顯示在「我的項目」頁面中。出於某種原因,它不顯示任何東西。請幫助?的ListView不顯示信息

新項目頁面:

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時候,你在做onCreateViewlistItems= new ArrayList<String>()。它沒有明確的數據。

而當你每次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("")添加數據

這種方式不是很恰當不過,但它應該工作。