我无法将创建的其他应用程序中的WORKING ListView转换为ListFragment
我已经摆脱了大多数错误,但仍有一些仍然困扰着我。我评论了我得到错误的地方以及它们的含义。
有人可以看看这个,然后告诉我我想念的是什么。
谢谢!!!
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());
}
}
最佳答案
更改
new Intent(this, MilageDetailActivity.class);
至
new Intent(getActivity(), MilageDetailActivity.class);
该意图需要上下文,并且片段不是从上下文派生的。
其他错误也相同。用getActivity()替换“ this”,以便传递正确的上下文。