我在片段中调用方法时遇到问题。
调用方法:public static List<mobAppModel> getmobAppModel(Context ctx)
方法所在的我的活动:
package com.example.eronetmarket;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import com.example.eronetmarket.mobAppModel;
import android.content.Context;
public class XMLsourcePullParser {
static final String KEY_APP_INFO = "appInfo";
static final String KEY_ID = "id";
static final String KEY_CATEGORY = "category";
static final String KEY_ICON_URL = "iconUrl";
static final String KEY_APP_HEADLINE = "appHeadline";
static final String KEY_APP_DESC = "appDesc";
static final String KEY_RATING_POINTS = "ratingPoints";
static final String KEY_PLATFORM = "platform";
static final String KEY_STORE_URL = "storeUrl";
static final String KEY_STORE_PRICE = "storePrice";
static final String KEY_DEVELOPER = "developer";
public static List<mobAppModel> getmobAppModel(Context ctx) {
// List of StackSites that we will return
List<mobAppModel> appModels;
appModels = new ArrayList<mobAppModel>();
// temp holder for current StackSite while parsing
mobAppModel curmobAppModel = null;
// temp holder for current text value while parsing
String curText = "";
try {
// Get our factory and PullParser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
// Open up InputStream and Reader of our file.
FileInputStream fis = ctx.openFileInput("XMLsource.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
// point the parser to our file.
xpp.setInput(reader);
// get initial eventType
int eventType = xpp.getEventType();
// Loop through pull events until we reach END_DOCUMENT
while (eventType != XmlPullParser.END_DOCUMENT) {
// Get the current tag
String tagname = xpp.getName();
// React to different event types appropriately
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase(KEY_APP_INFO)) {
// If we are starting a new <site> block we need
//a new StackSite object to represent it
curmobAppModel = new mobAppModel();
}
break;
case XmlPullParser.TEXT:
//grab the current text so we can use it in END_TAG event
curText = xpp.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase(KEY_APP_INFO)) {
// if </appInfo> then we are done with current app
// add it to the list.
appModels.add(curmobAppModel);
} else if (tagname.equalsIgnoreCase(KEY_ID)) {
curmobAppModel.setid(curText);
} else if (tagname.equalsIgnoreCase(KEY_CATEGORY)) {
curmobAppModel.setcategory(curText);
} else if (tagname.equalsIgnoreCase(KEY_ICON_URL)) {
curmobAppModel.seticonUrl(curText);
} else if (tagname.equalsIgnoreCase(KEY_APP_HEADLINE)) {
curmobAppModel.setappHeadline(curText);
}
else if (tagname.equalsIgnoreCase(KEY_APP_DESC)) {
curmobAppModel.setappDesc(curText);
}
else if (tagname.equalsIgnoreCase(KEY_RATING_POINTS)) {
curmobAppModel.setratingPoints(curText);
}
else if (tagname.equalsIgnoreCase(KEY_PLATFORM)) {
curmobAppModel.setplatform(curText);
}
else if (tagname.equalsIgnoreCase(KEY_STORE_URL)) {
curmobAppModel.setstoreURL(curText);
}
else if (tagname.equalsIgnoreCase(KEY_STORE_PRICE)) {
curmobAppModel.setstorePrice(curText);
}
else if (tagname.equalsIgnoreCase(KEY_DEVELOPER)) {
curmobAppModel.setdeveloper(curText);
}
break;
default:
break;
}
//move on to next iteration
eventType = xpp.next();
}
} catch (Exception e) {
e.printStackTrace();
}
// return the populated list.
return appModels;
}
}`
这是我的片段:
package com.example.eronetmarket;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
public class Preporuceno extends Fragment {
private AppAdapter mAdapter;
private ListView siteList;
private LinearLayout ll;
private FragmentActivity fa;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.activity_preporuceno, container, false);
siteList = (ListView) getActivity().findViewById(R.id.listView1);
siteList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
String url = mAdapter.getItem(pos).getstoreURL();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
if(isNetworkAvailable()){
Log.i("mobAppModel", "starting download Task");
AppDownloadTask download = new AppDownloadTask();
download.execute();
}else{
mAdapter = new AppAdapter(getActivity().getApplicationContext(), -1, XMLsourcePullParser.getmobAppModel(Preporuceno.this));
siteList.setAdapter(mAdapter);
}
return ll;}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private class AppDownloadTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
//Download the file
try {
Downloader.DownloadFromUrl("http://minores.info/joomla30/stacksites.xml", getActivity().openFileOutput("XMLsource.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result){
//setup our Adapter and set it to the ListView.
mAdapter = new AppAdapter(Preporuceno.this, -1, XMLsourcePullParser.getmobAppModel(Preporuceno.this));
siteList.setAdapter(mAdapter);
Log.i("mobAppModel", "adapter size = "+ mAdapter.getCount());
}
}
}
我在
收到错误XMLsourcePullParser.getmobAppModel(Preporuceno.this)
那
The method getmobAppModel(Context) in the type XMLsourcePullParser is not applicable for the arguments (Preporuceno)
我完全糊涂了: 由于活动工作正常,但作为片段,这是问题......
答案 0 :(得分:4)
Fragment没有扩展Context,因为他的上下文是活动在里面,所以你应该调用getmobAppModel(getActivity())。
答案 1 :(得分:0)
最简单的方法是使用一些事件总线,例如Square
中的Otto答案 2 :(得分:0)
使用此选项从片段中获取上下文:
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
myContext=activity;
}
然后使用你的方法:
XMLsourcePullParser.getmobAppModel(myContext)