错误转换查看传呼机细节JSON [英] Error converting view pager details to json

查看:137
本文介绍了错误转换查看传呼机细节JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我顺利地拿到了一个Viewpager的帮助工作[本教程:的http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/]

I successfully got a Viewpager working with help [from this tutorial: http://www.androidbegin.com/tutorial/android-viewpager-gallery-images-and-texts-tutorial/]

现在,我想在viewpager从网站上一个JSON文件中检索日期转换数据。我已经使用了code检索常规列表,它工作正常,但我不能与ViewPager的适配器确定问题导致此错误:

Now, I'm trying to converting the data in the viewpager to date retrieved from a json file on a website. I have used the code to retrieve a regular list and it works fine but a problem I can't pinpoint with the ViewPager's adapter is causing this error:

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

下面的片段中code:

Here's the code in the 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,这里的code:

And in the ViewPagerAdapter, here's the code:

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

}
}

我的猜测是这样的:
        公共ViewPagerAdapter(上下文的背景下,字符串[],其中,字符串[]日,
            的String []信息,INT []标志){
        this.context =背景;
        this.where =哪里;
        this.date =日期;
        this.information =信息;
        this.flag =标志;
    }

My guess is this: 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; }

从适配器引起的问题 - 这怎么可能是固定的,好吗?

From the adapter is causing the problem - How could it be fixed, please?

推荐答案

的问题是,你ViewPagerAdapter类有要求上下文语境构造,字符串[],其中,字符串[]日期的String []信息,INT [ ]标志 -

The problem is that your ViewPagerAdapter class has a constructor that asks for Context 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;
}

但你给活动,ArrayList的&LT; HashMap的&LT;字符串,字符串&GT;&GT;,字符串,字符串,字符串,字符串

您应该构造改变 -

You should change the constructor to -

public ViewPagerAdapter(Context context, ArrayList<HashMap<String,String>> map ,String s1,String s2,String s3,String s4) {
        ///implementation
    }

更多。

您应该改变这一行code的,现在是在for循环,这意味着你在每个迭代执行此操作时,把它放在外面forloop的:

You should change this line of code, now it is in the for loop, that means that you are doing this operation on each iteration, put it outside of the 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的 - 不使用此,它会给你的问题​​,如果之前的操作完成的活动都将丢失。退房改造或/和Okhttp,或至少使用AsyncTaskLoader。而对JSON操作,请GSON或杰克逊库。

AsyncTask - don't use this, it will give you problems if the activity is lost before the operation is complete. Check out Retrofit or/and Okhttp, or atleast use AsyncTaskLoader. And for Json manipulations, check out Gson or Jackson libraries.

这篇关于错误转换查看传呼机细节JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆