Click here to Skip to main content
15,868,127 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a Fragment Which has a Custom ListView and All the ListView Events is Handled in Fragment Class Itself. Now When I Click On The ListView Item I Am Populating Same ListView Again Based On Condition. But If I Press Back Button I Want to again Fill ListView on Previous State.

Is this Possible? If yes how?

Below is the Code For Fragment With ListView
Java
public class HomeFragment extends Fragment {

    String CourseName[]={"Special Courses","Engineering","BScIT","BScCS"};
    String UniversityEngArray[]={"Mumbai","Pune","Delhi","Rajasthan"};
    String UniversityBScITArray[]={"Mumbai","Pune","Delhi","Rajasthan"};
    Integer ImageId[]={
            R.drawable.letters,
            R.drawable.lettere,
            R.drawable.letterb,
            R.drawable.letterb
    };
    private ListView lv;
    CustomList customList;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        customList = new CustomList(getActivity(), CourseName, ImageId);
        lv = (ListView) rootView.findViewById(R.id.listView1);
        lv.setAdapter(customList);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                //Toast.makeText(getActivity(), "You Clicked at " + CourseName[+position], Toast.LENGTH_SHORT).show();
                if(CourseName[+position].toString().equalsIgnoreCase("Engineering")){
                    customList =new CustomList(getActivity(),UniversityEngArray,ImageId);
                    lv.setAdapter(customList);
                }
                else if(CourseName[+position].toString().equalsIgnoreCase("BScIT")){
                    customList =new CustomList(getActivity(),UniversityBScITArray,ImageId);
                    lv.setAdapter(customList);
                }
            }
        });

        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
Posted

1 solution

Of course it's possible, but then you should have a state machine with History. It's better to manipulate data, not UI. The UI should simple populate UI from data, say, data from history. As history may need a lot of data, you may need to persist it. Sorry, but further detail depends on what you have for data, the use of databases or something else, and so on.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900