在本教程的帮助下,我成功让 Viewpager 工作:http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/]
现在,我正在尝试将 viewpager 中的数据转换为从网站上的 json 文件检索到的日期。我已经使用代码来检索常规列表,它工作正常,但我无法通过 ViewPager 的适配器查明问题导致此错误:
Error:(159, 31) error: constructor ViewPagerAdapter in class ViewPagerAdapter cannot be applied to given types;
required: Context,String[],String[],String[],int[]
found: Activity,ArrayList<HashMap<String,String>>,String,String,String,String
reason: actual and formal argument lists differ in length
这是 fragment 中的代码:
public class WhatsHotFragment extends Fragment {
private TextView DateView;
private ImageView PhotoView;
private TextView whereView;
private TextView infoView;
ViewPager viewPager;
PagerAdapter adapter;
public WhatsHotFragment() {}
private static final String TAG_OS = "results";
private static final String where = "name";
private static final String date = "dates";
private static final String information = "info";
private static final String flag = "avatar_url";
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
private static String url = "http://celebirious.com/bey/data/style.json";
JSONArray android = null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_whats_hot, null);
oslist = new ArrayList<HashMap<String, String>>();
new JSONParse().execute();
return v;
}
class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
infoView = (TextView) getActivity().findViewById(R.id.info);
whereView = (TextView) getActivity().findViewById(R.id.where);
DateView = (TextView) getActivity().findViewById(R.id.date);
PhotoView = (ImageView) getActivity().findViewById(R.id.photo);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
android = json.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String infoView = c.getString(information);
String dateView = c.getString(date);
String whereView = c.getString(where);
String PhotoView = c.getString(flag);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(information, infoView);
map.put(date, dateView);
map.put(whereView, where);
map.put(PhotoView, flag);
oslist.add(map);
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
adapter = new ViewPagerAdapter(getActivity(), oslist, where, date, information, flag);
viewPager.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
在 ViewPagerAdapter 中,代码如下:
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
String[] where;
String[] date;
String[] information;
int[] flag;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, String[] where, String[] date,
String[] information, int[] flag) {
this.context = context;
this.where = where;
this.date = date;
this.information = information;
this.flag = flag;
}
@Override
public int getCount() {
return where.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView txtrank;
TextView txtcountry;
TextView txtpopulation;
ImageView imgflag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.styledeets, container,
false);
// Locate the TextViews in viewpager_item.xml
txtrank = (TextView) itemView.findViewById(R.id.date);
txtcountry = (TextView) itemView.findViewById(R.id.where);
txtpopulation = (TextView) itemView.findViewById(R.id.info);
// Capture position and set to the TextViews
txtrank.setText(where[position]);
txtcountry.setText(date[position]);
txtpopulation.setText(information[position]);
// Locate the ImageView in viewpager_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.photo);
// Capture position and set to the ImageView
imgflag.setImageResource(flag[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
我的猜测是这样的: 公共(public) ViewPagerAdapter(Context context, String[] where, String[] date, String[] 信息,int[] 标志) { this.context = 上下文; this.where = 哪里; this.date = 日期; this.信息 = 信息; this.flag = 标志; }
问题是由适配器引起的 - 请问如何解决?
最佳答案
问题是您的 ViewPagerAdapter 类有一个构造函数,要求 Context 上下文、String[] where、String[] date、String[] information、int[] flag -
public ViewPagerAdapter(Context context, String[] where, String[] date,
String[] information, int[] flag) {
this.context = context;
this.where = where;
this.date = date;
this.information = information;
this.flag = flag;
}
但是你正在给予Activity,ArrayList<HashMap<String,String>>,String,String,String,String
您应该将构造函数更改为 -
public ViewPagerAdapter(Context context, ArrayList<HashMap<String,String>> map ,String s1,String s2,String s3,String s4) {
///implementation
}
更多。
你应该改变这行代码,现在它在for循环中,这意味着你在每次迭代时都执行这个操作,将其放在for循环之外:
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String infoView = c.getString(information);
String dateView = c.getString(date);
String whereView = c.getString(where);
String PhotoView = c.getString(flag);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(information, infoView);
map.put(date, dateView);
map.put(whereView, where);
map.put(PhotoView, flag);
oslist.add(map);
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
adapter = new ViewPagerAdapter(getActivity(), oslist, where, date, information, flag);
viewPager.setAdapter(adapter);
}
更多:
AsyncTask - 不要使用这个,如果 Activity 在操作完成之前丢失,它会给你带来问题。查看 Retrofit 或/和 Okhttp,或者至少使用 AsyncTaskLoader。对于 Json 操作,请查看 Gson 或 Jackson 库。
关于java - 将查看寻呼机详细信息转换为 json 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30448667/