将视图寻呼机详细信息转换为json时出错

时间:2015-05-26 02:51:58

标签: java android json android-fragments

我成功获得了Viewpager的帮助[来自本教程:http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/]

现在,我正在尝试将viewpager中的数据转换为从网站上的json文件中检索到的日期。我已经使用代码检索常规列表,它工作正常,但我无法用ViewPager的适配器查明的问题导致此错误:

_UIKeyboardLayoutAlignmentView

这是片段中的代码:

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

在ViewPagerAdapter中,这是代码:

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();
        }

    }



}





 }

我的猜测是这样的:         public ViewPagerAdapter(Context context,String [] where,String [] date,             String [] information,int [] flag){         this.context = context;         this.where = where;         this.date = date;         this.information =信息;         this.flag = flag;     }

从适配器导致问题 - 怎么可以修复,好吗?

1 个答案:

答案 0 :(得分:2)

问题是你的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循环中,这意味着您在每次迭代时都执行此操作,将其放在forloop之外:

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 - 不要使用它,如果在操作完成之前活动丢失,它会给你带来问题。查看Retrofit或/和Okhttp,或至少使用AsyncTaskLoader。而对于Json操纵,请查看Gson或Jackson图书馆。