我对安卓很新。请帮我解决这个疑问。
我创建了一个图像滑块。 我有一个API,我想将这些API中的图像加载到滑块。
我创建了一个活动文件。
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);
}
}
}
这是一个页面适配器文件
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张图像。
我能够从活动类中的json解析中获取数据但我想在页面适配器类中设置数据,因为它显示了我的滑块。
那么如何将我在活动文件中获得的值传递给页面适配器类???
请帮助......
答案 0 :(得分:0)
您可以使用构造函数传递这些值。
PagerAdapter adapter = new MoreImageAdapter(MoreImage.this, String images, ArrayList<Sting> something,...);
然后在MoreImageAdapter中从构造函数中获取这些图像
public MoreImageAdapter(Context context, String images, ArrayList<String> something,...){
this.context = context;
this.images = images;
this.something = new ArrayList<String>(something);
}
您可以发送您想要发送的内容并从构造函数中获取。