gpt4 book ai didi

android - ListView Activity 到 ListFragment 与 SQLite DB

转载 作者:行者123 更新时间:2023-11-29 21:31:34 24 4
gpt4 key购买 nike

我在将我创建的另一个应用程序的工作 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);

Intent 需要上下文, fragment 不是从上下文派生的。

关于其他错误的相同问题。用 getActivity() 替换“this”,以便传递正确的上下文。

关于android - ListView Activity 到 ListFragment 与 SQLite DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19345899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com