• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Fragments with support package problem.

I'm having issues getting fragments to work with the support package. I've been reading a book on fragments and it's example is a to do list, but the examples provided are for V3 and above. Here's my code!

I'm having an issue with the "setListAdapter" I'm getting an error message saying it's "undefined for type fragment"
I'm all around new to android so I would really appreciate some help!

MAIN.java
Code:
package to.dolist.Ch4;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.widget.ArrayAdapter;

public class ToDoListActivity extends FragmentActivity implements NewItemFragment.OnNewItemAddedListener {
   
   private ArrayList<String> todoItems = new ArrayList<String>();
    private ArrayAdapter<String> aa;
   
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        FragmentManager fm = getSupportFragmentManager();
        Fragment todoListFragment = (Fragment)fm.findFragmentById(R.id.TodoListFragment);
        
        todoItems = new ArrayList<String>();
   
        
        todoListFragment.setListAdapter(new ArrayAdapter<String>(this, 
                       android.R.layout.simple_list_item_1, todoItems));
          
    }
    
    public void onNewItemAdded(String newItem)
    {
       todoItems.add(newItem);
       aa.notifyDataSetChanged();
    }
}
Fragment.java
Code:
package to.dolist.Ch4;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class NewItemFragment extends Fragment {

  private OnNewItemAddedListener onNewItemAddedListener;
  
  public interface OnNewItemAddedListener {
    public void onNewItemAdded(String newItem);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.new_item_fragment, container, false);

    final EditText myEditText = 
      (EditText)view.findViewById(R.id.myEditText);

    myEditText.setOnKeyListener(new View.OnKeyListener() {
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
        if (event.getAction() == KeyEvent.ACTION_DOWN)
          if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
              (keyCode == KeyEvent.KEYCODE_ENTER)) {
            String newItem = myEditText.getText().toString();
            onNewItemAddedListener.onNewItemAdded(newItem);
            myEditText.setText("");
            return true;
          }
        return false;
      }
    });

    return view;
  }
  

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
      
    try {
      onNewItemAddedListener = (OnNewItemAddedListener)activity;
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString() + 
                " must implement OnNewItemAddedListener");
    }
  }
}
 
Back
Top Bottom