1

I have two views in my Android application (2 view fragments actually). The first view has a textfield and a button, the 2nd view has a ListView.

What i want to do is set some data in the ListView, based on what is set in the textfield (from the first view). But i'm not sure how to do this.

I'm currently setting hardcoded data in the ListView from the onCreateView method in the 2nd Views SecondFragment.java file. Which looks something like this:

ListView list = (ListView)view.findViewById(R.id.list_mainmenu);

items.add(new SectionItem("Section 1"));
items.add(new EntryItem("Item 1", "This is item 1", 1));
items.add(new EntryItem("Item 2", "This is item 2", 2));
items.add(new EntryItem("Item 3", "This is item 3", 3));


EntryAdapter adapter = new EntryAdapter(this.getActivity().getBaseContext(), items);
list.setAdapter(adapter);

But having that in the onCreateView method isn't what i want. I want to be able to set that list for that view from another java file. In my case FirstFragment.java.

Is this possible, and if so, how can i do that?

I can imagine that there must already be some instance of the SecondFragment.java class, because the onCreateView is immidiately called when the app loads. I just can't find out where i can get it from.

Rob
  • 4,927
  • 12
  • 49
  • 54
Vivendi
  • 20,047
  • 25
  • 121
  • 196

1 Answers1

0

The thing is that you set the adapter in oncreate, rather setting it in oncreate, set it on a button click , or on time when you want to display the list like.

// here SelectOption is a button
SelectOption.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {    

        if(Text.equals("BEVERAGES")) {         
            l1.setAdapter(bever);
            return;
        }    
        else if(Text.equals("BREAKFAST")) {
            l1.setAdapter(Breakf);
            S5=new String[]{ Breakf.size() };
            return;
        }
        else if(Text.equals("LUNCH")) {
            l1.setAdapter(lunc);
            S5=new String[]{ lunc.size() };
            return;
        }
        else if(Text.equals("DINNER")) {
            l1.setAdapter(Dinn);
            S5=new String[]{ Dinn.size() };
            return;
        }
        else if(Text.equals("DESSERTS")) {
            l1.setAdapter(Dessert);
            S5=new String[]{ Dessert.size() };
            return;
        }
        else if(Text.equals("APPETIZERS & SIDES")) {
            l1.setAdapter(Appet);
            S5=new String[]{ Appet.size() };
            return;
        }
    }
});

so data list shall be populated only when you set an adapter , and after this when you add new values to adapter also add this line also

adapter.notifyDataSetChanged();
Rob
  • 4,927
  • 12
  • 49
  • 54
Avi Kumar
  • 4,403
  • 8
  • 36
  • 67