gpt4 book ai didi

android - 使用 AsyncTaskLoader 和 Fragment 的 onCreateLoader 方法中的不兼容类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:46:17 33 4
gpt4 key购买 nike

我考虑了一些使用 AsyncTaskLoader 和 Fragment (https://gist.github.com/codeswimmer/884591) 的例子,我也尝试这样做。但现在我无法修复不兼容的类型错误 - 在方法 onCreateLoader 中。我不知道为什么会这样,因为我也这样做过。

FeedLoader.java

public class FeedLoader extends AsyncTaskLoader<ArrayList<RSSItemData>> {

ArrayList<RSSItemData> listData;
Context ctx;
RSSItemData rssItem;
final Integer HTTP_CONNECTION_TIMEOUT = 15;
DBHelper dbHelper;


public FeedLoader(Context context) {
super(context);
this.ctx = context;
dbHelper = new DBHelper(ctx);
}


@Override
public ArrayList<RSSItemData> loadInBackground() {

listData = new ArrayList<RSSItemData>();

if(!BaseUtils.isNetworkAvailable(ctx)) return getNewsFromDB();

if(doWeHaveAnyNews()){

Log.d("myLogs", "We have news!");

String data = JSONUtils.getJSON("http://I-changed-URL.com", HTTP_CONNECTION_TIMEOUT*1000, ctx);
JSONResult result = new Gson().fromJson(data, JSONResult.class);
listData = result.items;

deleteAllNewsFromDB();
putNewsIntoDB(listData);

}
else{

Log.d("myLogs", "No news, retrieve from database");
listData = getNewsFromDB();

}

Log.d("myLogs", "Size of listData - " + listData.size());

return listData;
}

public boolean doWeHaveAnyNews() {...}
public void putNewsIntoDB(ArrayList<RSSItemData> listData) {...}
public void deleteAllNewsFromDB() {...}
public ArrayList<RSSItemData> getNewsFromDB() {...}

}

检索FeedFragment.java

public class RetrieveFeedFragment extends Fragment implements  LoaderManager.LoaderCallbacks<ArrayList<RSSItemData>> {

private RecyclerView rvNews;
LinearLayout progBarLinearLayout;
ProgressBar progBar;
TextView progBarText;

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

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

Log.d("myLogs", "Fragment onCreate");
}

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

Log.d("myLogs", "Fragment onCreateView");

View v = inflater.inflate(R.layout.news_list_fragment, null);

rvNews = (RecyclerView) v.findViewById(R.id.listViewNews);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rvNews.setLayoutManager(llm);
rvNews.setHasFixedSize(true);

rvNews = (RecyclerView) v.findViewById(R.id.listViewNews);

progBarLinearLayout = (LinearLayout) v.findViewById(R.id.progressBarLinearLayout);
progBar = (ProgressBar) v.findViewById(R.id.progressBar);
progBarText = (TextView) v.findViewById(R.id.progressBarText);

return v;

}


@Override
public Loader<ArrayList<RSSItemData>> onCreateLoader(int id, Bundle args) {
return new FeedLoader(getActivity());
/* Incompatible types ERROR
Required - anroid.support.v4.content.Loader <java.util.ArrayList<com.project.myproject.RetrieveFeed.RSSItemData>>
Found - com.project.myproject.RetrieveFeed.FeedLoader
*/
}

@Override
public void onLoadFinished(Loader<ArrayList<RSSItemData>> loader, ArrayList<RSSItemData> listData) {
// RSSAdapter = new RSSAdapter(listData, getActivity());
// rvNews.setAdapter(RSSAdapter);
// progBarLinearLayout.setVisibility(LinearLayout.GONE);
Log.d("myLogs", "onLoadFinished");
}
@Override
public void onLoaderReset(Loader<ArrayList<RSSItemData>> loader) {
Log.d("myLogs", "onLoaderReset");
}
}

MainActivity.java(以防万一)

public class MainActivity extends FragmentActivity {

RetrieveFeedFragment fragment;;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if(savedInstanceState == null){
fragment = new RetrieveFeedFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_news, fragment, "retrieve_feed_tag").commit();
Log.d("myLogs", "New Fragment in Activity");
}
else{
fragment = (RetrieveFeedFragment) getSupportFragmentManager().findFragmentByTag("retrieve_feed_tag");
Log.d("myLogs", "Get old Fragment by tag in Activity");
}

}
}

最佳答案

检查您的导入。确保您使用的是
android.support.v4.app.LoaderManager.LoaderCallbacks<D>
而不是
android.app.LoaderManager.LoaderCallbacks<D>
在你的 fragment 和 android.support.v4.content.AsyncTaskLoader<D>在你的FeedLoader

关于android - 使用 AsyncTaskLoader 和 Fragment 的 onCreateLoader 方法中的不兼容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38099873/

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