johnbob
johnbob

Reputation: 57

List in Fragment

I have some issues with a listview in a Fragment. I want to show a List with Fruits in the Fragment, tried a few things but. In a later version the String[] should contain data from a json object.

This is the current status:

public class fragmentA extends Fragment
    implements View.OnClickListener {

TextView textView;
ViewStub viewStub;
ListFragment listView;

static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
        "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
        "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {
     // das Layout fuer dieses Fragment laden
     View view= inflater.inflate(R.layout.fragmenta, container, false);
     // inflate layout
     Button notfallbtn = (Button) view.findViewById(R.id.notfallbtn);

    textView = (TextView) view.findViewById(R.id.textView);
   // listView = (ListFragment) view.findViewById(R.id.einkaufsliste);
    viewStub = (ViewStub) view.findViewById(R.id.viewStub);
    viewStub.setVisibility(View.GONE);
    // initialize button using the inflated view object
    notfallbtn.setOnClickListener(this);
    // listener for button

   setListAdapter(new ArrayAdapter<String>(this, R.layout.einkaufsliste,FRUITS));

   ListView listView = getListView();
   listView.setTextFilterEnabled(true);

   listView.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> parent, View view,
                               int position, long id) {
           // When clicked, show a toast with the TextView text
           Toast.makeText(getApplicationContext(),
                   ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
       }
   });


    return view;   // return inflated view
 }

The error messages I get:

Cannot resolve 'setListAdapter'

Cannot resolve 'einkaufsliste'

Any help will be greatly appreciated.

Upvotes: 0

Views: 219

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

public class fragmentA extends Fragment

does not extend ListFragment. setListAdapter is a method of ListFragment.

http://developer.android.com/reference/android/app/ListFragment.html

Also you probably need

new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,FRUITS)

Instead of this use getActivity() which returns the activity this fragment is associated with.

Also read

http://developer.android.com/reference/android/app/ListActivity.html

Also override onActivityCreated and use ListView lv = getListView().

Upvotes: 1

Related Questions