1

我無法從我到ListFragment的ListView活動與SQL精簡版數據庫ListFragment

我已經擺脫了最錯誤的產生花葯應用程序轉換工作的ListView,但我有幾個仍在困擾我。我已經評論了我在哪裏得到錯誤以及它們是什麼。

有人可以看看這個並告訴我我失蹤了。

謝謝!

package com.mycomp.myapp; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import android.app.Dialog; 
import android.app.ListActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v4.app.ListFragment; 
import android.support.v4.app.LoaderManager; 
import android.support.v4.content.CursorLoader; 
import android.support.v4.content.Loader; 
import android.view.ContextMenu; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.view.View.OnClickListener; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView.AdapterContextMenuInfo; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

import com.mycomp.myapp.contentprovider.TT_MilageContentProvider; 
import com.mycomp.myapp.database.TT_MilageTable; 

public class MilageFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { 

     private static final int ACTIVITY_CREATE = 0; 
     private static final int ACTIVITY_EDIT = 1; 
     private static final int DELETE_ID = Menu.FIRST + 1; 
     private SimpleCursorAdapter adapter; 
     private static int dividerHeight = 2; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      fillData(); 
      registerForContextMenu(getListView()); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
       View rootView = inflater.inflate(R.layout.milage, container, false); 
       ListView listView = (ListView) rootView.findViewById(R.id.list); 
       return rootView; 
     } 

     public boolean onCreateOptionsMenu(Menu menu) { 
      MenuInflater inflater = getActivity().getMenuInflater(); 
      inflater.inflate(R.menu.main, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      switch (item.getItemId()) { 
      case R.id.insert: 
       createItem(); 
       return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 

     public boolean onContextItemSelected(MenuItem item, Context context) { 
      switch (item.getItemId()) { 
      case DELETE_ID: 
       AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
       Uri uri = Uri.parse(TT_MilageContentProvider.CONTENT_URI + "/" + info.id); 
       context.getContentResolver().delete(uri, null, null); 
       fillData(); 
       return true; 
      } 
      return super.onContextItemSelected(item); 
     } 




     // ***** IT'S TELLING ME TO REMOVE THE ARGUMENTS TO MATCH 'INTENT' ***** 
     //  new Intent(this, MilageDetailActivity.class); 

     private void createItem() { 
      Intent i = new Intent(this, MilageDetailActivity.class); 
      startActivityForResult(i, ACTIVITY_CREATE); 
     } 






     // ***** IT'S TELLING ME TO REMOVE THE ARGUMENTS TO MATCH 'INTENT' ***** 
     //  new Intent(this, MilageDetailActivity.class); 

     @Override 
     public void onListItemClick(ListView l, View v, int position, long id) { 
      super.onListItemClick(l, v, position, id); 
      Intent i = new Intent(this, MilageDetailActivity.class); 
      Uri todoUri = Uri.parse(TT_MilageContentProvider.CONTENT_URI + "/" + id); 
      i.putExtra(TT_MilageContentProvider.CONTENT_ITEM_TYPE, todoUri); 

      startActivityForResult(i, ACTIVITY_EDIT); 
     } 

     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
      super.onActivityResult(requestCode, resultCode, intent); 
     } 



      // ******* The Error is..... 
      // The constructor SimpleCursorAdapter(MilageFragment, int, null, String[], int[], int) is undefined on 
      // new SimpleCursorAdapter(this, R.layout.milage_row, null, from, to, 0); 

     private void fillData() { 
      String[] from = new String[] { TT_MilageTable.COLUMN_SUMMARY }; 
      int[] to = new int[] { R.id.label }; 

      getLoaderManager().initLoader(0, null, this); 
      adapter = new SimpleCursorAdapter(this, R.layout.milage_row, null, from, to, 0); 

      setListAdapter(adapter); 
      } 

     @Override 
      public void onCreateContextMenu(ContextMenu menu, View v, 
       ContextMenuInfo menuInfo) { 
      super.onCreateContextMenu(menu, v, menuInfo); 
      menu.add(0, DELETE_ID, 0, R.string.menu_delete); 
      } 




      // ******* The Error is..... 
      // The constructor CursorLoader(MilageFragment, Uri, String[], null, null, null) is undefined and to 
      // Remove Arguments to match 'CursorLoader(Context)' 

     public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
      String[] projection = { TT_MilageTable.COLUMN_ID, TT_MilageTable.COLUMN_SUMMARY }; 
      CursorLoader cursorLoader = new CursorLoader(this, TT_MilageContentProvider.CONTENT_URI, projection, null, null, null); 
      return cursorLoader; 
     } 

     public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 
      adapter.swapCursor(data); 
     } 

     public void onLoaderReset(Loader<Cursor> loader) { 
      adapter.swapCursor(null); 
     } 

     @Override 
     public void onDestroy() { 
       super.onDestroy(); 
       android.os.Process.killProcess(android.os.Process.myPid()); 
      } 
     } 

回答

2

變化

new Intent(this, MilageDetailActivity.class); 

new Intent(getActivity(), MilageDetailActivity.class); 

意圖需要一個上下文和片段不從上下文推導。

對其他錯誤也是同樣的問題。將「this」替換爲getActivity(),以便傳遞正確的上下文。

+0

你的真棒!非常感謝你!! – Sobo

相關問題