Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android viewPager-需要从JSON加载图像_Android_Android Viewpager - Fatal编程技术网

Android viewPager-需要从JSON加载图像

Android viewPager-需要从JSON加载图像,android,android-viewpager,Android,Android Viewpager,我正在尝试创建一个ImageView,更好的说法是一个带有3个图像的Viewpager滑块,它应该从左向右滑动 Im my singleitemview.xml我有一个我放在那里的ViewPager: <RelativeLayout android:id="@+id/container" android:layout_width="match_parent" android:layo

我正在尝试创建一个ImageView,更好的说法是一个带有3个图像的Viewpager滑块,它应该从左向右滑动

Im my singleitemview.xml我有一个我放在那里的ViewPager:

      <RelativeLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/pager"
                    android:layout_width="fill_parent"
                    android:layout_height="100dp" />
</RelativeLayout>

 <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:id="@+id/sometext"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/sometext"
                  />
</RelativeLayout>
}

最后是pager_item.xml:

问题是,图像是在mResources内的CustomPagerAdapter中定义的,但我需要使用json中的图像,这是我从json获取数据时在singleitemview.java中才知道的

你能帮我修改代码让它工作吗

在适配器的构造函数中传递从JSON获得的图像列表


}

缺少setContentViewR.layout.SingleItemViews抱歉,它在那里,我刚刚缩短了代码,您的代码中没有错误问题可能在您的活动xml文件中,您是否可以放置完整的singleitemview.xml我想我发现了第一个问题-当我将viewpager高度设置为dp:android:layout_height=100dp时,图像显示出来了!所以这是高度问题,但现在我需要解决如何将来自json的图像放在那里,因为当前图像是在adapteryes中硬编码的,就是这样!非常感谢你。然后在我的singleitemview.java中,我只添加了mResources.addimg1;mResources.addimg2;并初始化mCustomPagerAdapter=new CustomPagerAdapterthis,mResources;这很好用。再次感谢你的帮助。
public class SingleitemView extends AppCompatActivity {

    CustomPagerAdapter mCustomPagerAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.singleitemview);
//... here I get some data from URL as json
//and then calling this:

createMarkersFromJson(json.toString());
}

private void createMarkersFromJson(String json) throws JSONException {

        JSONObject jsonObj = new JSONObject(json);
        JSONArray actors = jsonObj.getJSONArray("result");

        for (int i = 0; i < actors.length(); i++) {
            final JSONObject c = actors.getJSONObject(i);

            String typ = c.getString("typ");
            final String place2 = c.getString("place");

            final String img1 = c.getString("img1");
            final String img2 = c.getString("img2");
            final String img3 = c.getString("img3");

//Picasso.with(this).load(img1).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imgimg1);

            mCustomPagerAdapter = new CustomPagerAdapter(this);

            ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mCustomPagerAdapter);
}
}
class CustomPagerAdapter extends PagerAdapter {
int[] mResources = {
        R.drawable.bottom2,
        R.drawable.bottom,
        R.drawable.hrady

};
Context mContext;
LayoutInflater mLayoutInflater;

public CustomPagerAdapter(Context context) {
    mContext = context;
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return mResources.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((LinearLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

    ImageView imageView = (ImageView) itemView.findViewById(R.id.img1);
    imageView.setImageResource(mResources[position]);

    container.addView(itemView);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout) object);
}
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:foregroundGravity="top|center"
        android:scaleType="fitCenter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/img1" />

</LinearLayout>
class CustomPagerAdapter extends PagerAdapter {   
List<String> mResources = new ArrayList<>();

Context mContext;
LayoutInflater mLayoutInflater;

public CustomPagerAdapter(Context context , List<String> mResources) {
    mContext = context;
    this.mResources=mResources;
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return mResources.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((LinearLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

    ImageView imageView = (ImageView) itemView.findViewById(R.id.img1);
    Picasso.with(this).load(mResources.get(position)).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(imageView);

    container.addView(itemView);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout) object);
}