gpt4 book ai didi

android - 方法 initLoader(int, Bundle, LoaderManager.LoaderCallbacks) 一直给我不适用的错误

转载 作者:行者123 更新时间:2023-11-29 17:48:54 25 4
gpt4 key购买 nike

我卡在我的代码中,我正在使用抽屉布局,而在 fragment 布局上,我想在从数据库中获取人员列表后显示它。在 fragment 类中,当我使用游标适配器查询并将其设置为列表适配器时,错误显示:

类型 LoaderManager 中的方法 initLoader(int, Bundle, LoaderManager.LoaderCallbacks) 不适用于参数 (int, null, MainFragment)。

我哪里错了,请帮忙。如果您需要更多信息,请询问。下面是我的 fragment 类及其相应的布局代码。提前致谢。

fragment 类:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bnkinfotech.corporatedirectory.Directory.Mobile;
import com.bnkinfotech.corporatedirectory.Directory.Telephone;

import android.app.ListFragment;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
//import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.CursorAdapter;


public class MainFragment extends ListFragment implements LoaderCallbacks<Cursor> {

String loginPref;
String email;
String updatedDate;
String onlineemail = null;
SharedPreferences prefAccount;
protected Cursor cursor;
protected ListAdapter adapter;
protected SQLiteDatabase db;
public static String ArgAccountNumber = "account_list_number";

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


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

prefAccount = getActivity().getSharedPreferences("CoDiAccount", Context.MODE_PRIVATE);
onlineemail = prefAccount.getString("email", "-1").toLowerCase();
String directoryname = "CoDiApp" + onlineemail;
SharedPreferences prefs = getActivity().getSharedPreferences(directoryname, Context.MODE_PRIVATE);
loginPref = prefs.getString("login", "-1");
email = prefs.getString("email", "-1").toLowerCase();
updatedDate = prefs.getString("lastupdateddate", "-1");

View rootView = inflater.inflate(R.layout.main_fragment, container, false);
return rootView;
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}

@Override
public void onStart() {
super.onStart();
if (loginPref == "1")
{
showContacts();
}
else if (loginPref == "0")
{
//Validate account
Intent verifyAccount = new Intent(getActivity(), VerifyAccountActivity.class);
getActivity().startActivity(verifyAccount);
}
else
{
//Email Login
Intent checkEmail = new Intent(getActivity(), CheckEmailActivity.class);
getActivity().startActivity(checkEmail);
}
}

public int showContacts() {
SharedPreferences prefAccount = getActivity().getSharedPreferences("CoDiAccount", Context.MODE_PRIVATE);
String onlineemail = prefAccount.getString("email", "-1").toLowerCase(Locale.ENGLISH);
String directoryname = "CoDiApp" + onlineemail;
SharedPreferences prefs = getActivity().getSharedPreferences(directoryname, Context.MODE_PRIVATE);

String loadxml = prefs.getString("loadxml", "-1");
String groupBy = prefs.getString("groupby", "0");
if (loadxml == "-1")
{
LoadXMLFile();
Editor prefEditor = prefs.edit();
prefEditor.putString("loadxml", "1");
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
prefEditor.putString("lastupdateddate", timeStamp.toString());
prefEditor.commit();
}

String[] uiBindFrom = { SQLiteDB.FIRST_NAME, SQLiteDB.LAST_NAME };
int[] uiBindTo = { R.id.firstName, R.id.lastName };

getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(
getActivity().getApplicationContext(), R.layout.main_fragment,
null, uiBindFrom, uiBindTo,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
return 1;
}

public void LoadXMLFile() {
String response = "";
RequestManager rm = new RequestManager();
String x = "";
ArrayList<Mobile> arrMob;
ArrayList<Telephone> arrTel;

try
{
if(isValidEmail(email))
{
String url = String.format("GetDirectory/{0}", email);
response = rm.GetResponse(url);

try {
// Create your Json Array
JSONArray lList=new JSONArray(response);
// Iterate the json array to get each Json object
for (int i = 0; i < lList.length(); i++) {

JSONObject lObject=lList.getJSONObject(i);

String fName=lObject.getString("FirstName");
String lName=lObject.getString("LastName");
String gName=lObject.getString("GroupName");

JSONArray jsonArr= lObject.getJSONArray("MobileNumbers");
Mobile[] mNumbers=new Mobile[jsonArr.length()];
arrMob = new ArrayList<Mobile>();
for(int j=0;j<jsonArr.length();j++)
{
mNumbers[j]= (Mobile) jsonArr.get(j);
arrMob.add(mNumbers[j]);
}

JSONArray jsonArrTel= lObject.getJSONArray("TelephoneNumbers");
Telephone [] tNumbers=new Telephone[jsonArrTel.length()];
arrTel = new ArrayList<Telephone>();
for(int k=0;k<jsonArrTel.length();k++)
{
tNumbers[k]=(Telephone) jsonArrTel.get(k);
arrTel.add(tNumbers[k]);
}
String hAddress=lObject.getString("HomeAddress");
String oAddress=lObject.getString("OfficeAddress");

//Code to Insert in Sqlite database .....
Directory dir = new Directory();
dir.setFirstName(fName);
dir.setLastName(lName);
dir.setGroupName(gName);
dir.setMobiles(arrMob);
dir.setTelephones(arrTel);
dir.setHomeAddress(hAddress);
dir.setOfficeAddress(oAddress);

DatabaseHandler dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
SQLiteDatabase sqliteDatabase = dbHandler.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put(DatabaseHandler.ID, dir.getId());
contentValues.put(DatabaseHandler.FIRST_NAME, dir.getFirstName());
contentValues.put(DatabaseHandler.LAST_NAME, dir.getLastName());
contentValues.put(DatabaseHandler.GROUP_NAME, dir.getGroupName());

contentValues.put(DatabaseHandler.MOBILES, dir.getMobiles().toString());
contentValues.put(DatabaseHandler.TELEPHONES, dir.getTelephones().toString());
contentValues.put(DatabaseHandler.HOME_ADDRESS, dir.getHomeAddress());
contentValues.put(DatabaseHandler.OFFICE_ADDRESS, dir.getOfficeAddress());

sqliteDatabase.insert(DatabaseHandler.TABLE_NAME_MEMBER, null, contentValues);
sqliteDatabase.close();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
else
{
String msg = "Sorry, invalid email";
Toast.makeText(getActivity().getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}
catch (Exception ex)
{
response = ex.getMessage().toString();
}
}
public final static boolean isValidEmail(CharSequence email) {
if (email == null) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
}

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
String[] projection = { SQLiteDB.ID, SQLiteDB.FIRST_NAME, SQLiteDB.LAST_NAME };

CursorLoader cursorLoader = new CursorLoader(getActivity(),
DatabaseAccessUtility.CONTENT_URI, projection, null, null, null);
return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
((SimpleCursorAdapter) adapter).swapCursor(arg1);
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
((SimpleCursorAdapter) adapter).swapCursor(null);
}

}

MainFragment 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25dp"
android:minHeight="25dp"
android:id="@+id/FlyOutContent">
<LinearLayout
android:id="@+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ProgressBar
android:id="@+id/pbHeaderProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp" >

<TextView
android:id="@+id/firstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First" />

<TextView
android:id="@+id/lastName"
android:layout_marginLeft="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/firstName"
android:text="Last" />

</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:minWidth="25dp"
android:minHeight="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/empty">
<TextView
android:text="@string/NoFavoritesText"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/textView1"
android:gravity="center"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:textColor="#ff5a5a5a"
android:layout_marginTop="80dip" />
</LinearLayout>
</LinearLayout>

最佳答案

由于您使用的是支持库,因此请改用:

getActivity().getSupportLoaderManager().initLoader(0, null, this);

关于android - 方法 initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) 一直给我不适用的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24454018/

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