首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将值从活动传递到页面适配器

如何将值从活动传递到页面适配器
EN

Stack Overflow用户
提问于 2015-05-07 20:28:16
回答 1查看 1.6K关注 0票数 1

我对android还是个新手。请帮我解决这个疑问。

我已经创建了一个图像滑块。我有一个API,我想从这些API加载到滑块的图像。

我已经创建了一个活动文件。

代码语言:javascript
复制
public class MoreImage extends Activity {


    ImageView ProductImage;
    private ConnectionDetector cd;
    UserFunctions userFunction;
    ProgressDialog mProgressDialog;
    JSONObject jsonobject,jsonobj1;
    JSONObject jsonobj_prodPriceDetails, jsonobj_RUPEE;
    HListView listView;
    Button img_buy_now;



    String Vendor_id=null;

    SharedPreferences pref;
    String  Store_id_pref=null;
    String item_id_pref=null;
    EditText edt_search;
    public static Typeface font;
    String from=null;
    RelativeLayout  pb_progress;

    GridView related_grid;
    ImageView add_cart;
    String full_path_re;
    static TextView notifCount;

    ListView list;

    SrollviewAdapter scrAdapter;
    JSONArray jsonarray = null;
    JSONArray jsonarray1=null;
    JSONArray json_store_array = null;

    ArrayList<HashMap<String, String>> arraylist;
    ArrayList<HashMap<String, String>> store_arraylist;
    String images;


    static String PRODUCT_IMAGES = "image_list";
    static String COUNT = "count";
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);




          setContentView(R.layout.image_item);






          userFunction = new UserFunctions();
            cd = new ConnectionDetector(getApplicationContext());
            arraylist = new ArrayList<HashMap<String, String>>();

            new DownloadJSON().execute();



            pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            Vendor_id=pref.getString("user_id", null); 
            Store_id_pref=pref.getString("store_id", null); 
            item_id_pref = pref.getString("item_id", null);

            getActionBar().setDisplayHomeAsUpEnabled(true);
            setTitle("Product List");
            getActionBar().setIcon(R.drawable.ic_launcher);

            Intent myIntent = getIntent(); 
            from = myIntent.getStringExtra("from"); 

            font = Typeface.createFromAsset(getBaseContext().getAssets(),
                    "fonts/Amaranth-Regular.otf");
            if (cd.isConnectingToInternet()) {

            } 

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        } 
        public class DownloadJSON extends AsyncTask<Void, Void, Void> {

            protected void onPreExecute() {
                super.onPreExecute();

                //pb_progress.setVisibility(View.VISIBLE);

            }

            @Override
            protected Void doInBackground(Void... params) {

                jsonobject = userFunction.imageG("9");


                try {
                    // Locate the array name in JSON
                    jsonarray = jsonobject.getJSONArray("image_gallery");

                    jsonarray1 =jsonobj1.getJSONArray("count");

                    String s= jsonarray1.toString();

                    System.out.print("jsonarray1:::::"+jsonarray1);

                    int b= Integer.parseInt(s);

                    for (int i = 0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();

                        jsonobject = jsonarray.getJSONObject(i);



                        // Retrive JSON Objects
                      map.put("image_list", jsonobject.getString("image_list"));

                      map.put("count","jsonarray1");

                        arraylist.add(map);
                        System.out.print("all data::::::"+map);
                    }
                } catch (JSONException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void args) {
                ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
                PagerAdapter adapter = new MoreImageAdapter(MoreImage.this);
                viewPager.setAdapter(adapter);


                //pb_progress.setVisibility(View.GONE);


            }

        }



}

这是一个页面适配器文件

代码语言:javascript
复制
public class MoreImageAdapter  extends PagerAdapter{
    HashMap<String, String> resultp = new HashMap<String, String>();
   Context context;
    int count1;





//   int[] imageId = {
//         R.drawable.slide1,
//           R.drawable.slide2,
//           R.drawable.unfollow,
//           R.drawable.facebook_icon,
//           R.drawable.ic_launcher,
//           R.drawable.follow_unfollow,};

   public MoreImageAdapter(Context context){
       this.context = context;

   }


   @Override
   public Object instantiateItem(ViewGroup container, int position) {
       // TODO Auto-generated method stub

       LayoutInflater inflater = ((Activity)context).getLayoutInflater();

       View viewItem = inflater.inflate(R.layout.image_item, container, false);
       ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
       imageView.setImageResource(imageId[position]);


       ((ViewPager)container).addView(viewItem);

       return viewItem;
   }

   @Override
   public int getCount() {
       // TODO Auto-generated method stub
       return imageId.length;
   }

   @Override
   public boolean isViewFromObject(View view, Object object) {
       // TODO Auto-generated method stub

       return view == ((View)object);
   }


   @Override
   public void destroyItem(ViewGroup container, int position, Object object) {
       // TODO Auto-generated method stub
       ((ViewPager) container).removeView((View) object);
   }


}

有两个json对象,一个是image,另一个是count。图像包含1个以上的图像。

我可以在activity类中从json解析中获得数据,但我想在页面适配器类中设置数据,因为它显示了我的滑块。

那么如何将我在activity文件中获得的那些值传递给页面适配器类呢?

请帮帮忙......

EN

回答 1

Stack Overflow用户

发布于 2015-05-07 21:13:04

您可以使用构造函数来传递这些值。

代码语言:javascript
复制
PagerAdapter adapter = new MoreImageAdapter(MoreImage.this, String images, ArrayList<Sting> something,...);

然后在MoreImageAdapter中从构造器中获取这些图像

代码语言:javascript
复制
public MoreImageAdapter(Context context, String images, ArrayList<String> something,...){
   this.context = context;
   this.images = images;
   this.something = new ArrayList<String>(something);
}

您可以发送您想要发送的所有内容,并从构造函数中获取它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30101102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档