1

У меня есть ListFragment, который вызывается из Tab Navigation в ActionBar. У меня есть пользовательский адаптер для отображения двух разных текстовых просмотров для каждого элемента списка. У меня возникли проблемы с реализацией моего onItemClickListener. Вот мой ListFragment код:Включение onListItemClick в ListFragment

public class MaterialsListFragment extends SherlockListFragment { 

    public ERGAdapter db; 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
    ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.portrait_material_view, container, false); 
    } 

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

     // create new DBAdapter 
     db = new ERGAdapter(getActivity()); 

     // ---- get all records 
     db.open();  // Open database 

     // Get all of the notes from the database and create the item list 
     Cursor c = db.getAllRecords(); 

     String[] from = new String[] { ERGAdapter.KEY_IDNO, ERGAdapter.KEY_MATERIAL }; 
     int[] to = new int[] { R.id.idno, R.id.materials };   

     // Now create an array adapter and set it to display using our row 
     MyListAdapter materials = new MyListAdapter(getActivity(), R.layout.list_cell, c, from, to); 
     setListAdapter(materials); 

     // get the list view and the set the listener 
     ListView lv = getListView(); 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       Log.i("MATERIAL"," - position: "+Integer.toString(position)); 
      } 
     }); 
    } 

    public class MyListAdapter extends SimpleCursorAdapter { 

     public MyListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) { 
      super(context, layout , cursor, from, to); 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor cursor) { 


      // Create the idno textview with background image 
      TextView idno = (TextView) view.findViewById(R.id.idno); 
      String unidno = String.format("%1$.0f", cursor.getDouble(3)); 
      idno.setText(unidno); 

      // create the material textview 
      TextView materials = (TextView) view.findViewById(R.id.materials); 
      materials.setText(cursor.getString(1)); 
     } 
    } 
} 

Я получаю фатальное исключение с этой линией: ListView lv = getListView();

Вот это уместный LogCat:

05-25 16:32:45.802: E/AndroidRuntime(660): Caused by: java.lang.IllegalStateException: Content view not yet created 
05-25 16:32:45.802: E/AndroidRuntime(660): at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328) 
05-25 16:32:45.802: E/AndroidRuntime(660): at android.support.v4.app.ListFragment.getListView(ListFragment.java:222) 
05-25 16:32:45.802: E/AndroidRuntime(660): at com.cca.ergpro.MaterialsListFragment.onCreate(MaterialsListFragment.java:80) 

Я думаю, что мне нужно сделать это часть моего пользовательского CursorAdapter, но я не уверен, как действовать дальше. Любые предложения будут ценны.

ответ

Смежные вопросы