如何从JSON和图像从SD卡绑定图片叫什么名字? [英] How to Bind Image name from json and Images from sd card?

查看:124
本文介绍了如何从JSON和图像从SD卡绑定图片叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建的图像和关于图像的信息的幻灯片。我正在从JSON 图片名,并获得图片构成的SD卡。我要显示幻灯片一样ViewPager。我解析的JSON和存储到数据库。

的问题是

1)如何从特定名称的图像,因为我想要显示它的信息。

2)我想实施延迟加载 bcoz我有500+的图像和大小为900 + KB /图像。

下面是我的JSON格式

<$p$p><$c$c>[{\"Style_RefNo\":\"SJBG1175\",\"Style_GWT\":\"5.64\",\"Style_DWT\":\"0.27\",\"Style_CSWT\":\"0\",\"ImageName\":\"SJBG1175.jpg\",\"MainCatname\":\"BanglesBracelets\",\"MainCatid\":\"77\"}]

下面是我的code,我成功地实现视图寻呼机和图像都显示正常。但我面临的上述问题2,请帮助我

 公共类ViewRing扩展活动
{    //声明变量
    私人ProgressDialog PD;
    公共静态数据源数据源;
    清单&LT; GetStyleData&GT; styledata;
    ViewPager viewPager;
    PagerAdapter适配器;
    的String []排名;
    的String []国家;
    的String []人口;
    的String [] CSWT;
    的String []类;
    INT []标志;
    文件[]的文件列表;
    按钮btnHome;    ArrayList的&LT;串GT; F =新的ArrayList&LT;串GT;(); //文件路径列表    ArrayList的&LT;串GT; Personal Data个人纪录=新的ArrayList&LT;串GT;();
    ArrayList的&LT;串GT; GWT =新的ArrayList&LT;串GT;();
    ArrayList的&LT;串GT; DWT =新的ArrayList&LT;串GT;();
    ArrayList的&LT;串GT; CSWT =新的ArrayList&LT;串GT;();
    ArrayList的&LT;串GT;分类=新的ArrayList&LT;串GT;();    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        //从viewpager_main.xml视图
        的setContentView(R.layout.viewpager_main);
        数据源=新的DataSource(ViewRing.this);
        PD =新ProgressDialog(ViewRing.this);
        getFromSdcard();
        //生成样本数据        styledata = dataSource.getRingData();        的for(int i = 0; I&LT; styledata.size();我++)
        {
            透射电镜字符串prefNo = styledata.get(I).getStyle_RefNo();
            串tempGWT = styledata.get(ⅰ).getStyle_GWT();
            串tempDWT = styledata.get(ⅰ).getStyle_DWT();
            串tempCSWT = styledata.get(ⅰ).getStyle_CSWT();
            串tempCategory = styledata.get(ⅰ).getMainCatname();            refNo.add(TEM prefNo);
            GWT.add(tempGWT);
            DWT.add(tempDWT);
            CSWT.add(tempCSWT);
            Category.add(tempCategory);            Log.v(Personal Data个人纪录,+统prefNo);
            Log.v(GWT,+ tempGWT);
            Log.v(DWT的,+ tempDWT);
            Log.v(CSWT,+ tempCSWT);
            Log.v(类别,+ tempCategory);
        }
        //找到ViewPager在viewpager_main.xml
        viewPager =(ViewPager)findViewById(R.id.pager);        //通行证结果ViewPagerAdapter类
        适配器=新ViewPagerAdapter(这一点,军衔,国家,人口,CSWT,分类,标志);        //绑定适配器到ViewPager
        viewPager.setAdapter(适配器);
        btnHome =(按钮)findViewById(R.id.btnHome);
        btnHome.setOnClickListener(新OnClickListener()
        {
            @覆盖
            公共无效的onClick(视图v)
            {
                // TODO自动生成方法存根
                类RingTask扩展的AsyncTask&LT;太虚,太虚,对象&gt;
                {
                    @覆盖
                    在preExecute保护无效()
                    {
                        // TODO自动生成方法存根
                        super.on preExecute();
                        pd.setTitle(等一下!);
                        pd.setMessage(加载......);
                        pd.show();                    }
                    @覆盖
                    保护对象doInBackground(无效... PARAMS)
                    {
                        // TODO自动生成方法存根
                        意图iRing =新意图(ViewRing.this,Title.class);
                        startActivity(iRing);
                        返回null;
                    }                    @覆盖
                    保护无效onPostExecute(对象结果)
                    {
                        // TODO自动生成方法存根
                        super.onPostExecute(结果);
                        如果(pd.isShowing())
                        {
                            pd.dismiss();
                        }
                    }
                }
                新RingTask()执行();            }
        });
    }
    公共无效getFromSdcard()
    {
//文件文件=新的文件(android.os.Environment.getExternalStorageDirectory(),MapleBear);
        档案文件=新的文件(到/ mnt / SD卡/ FinalView /戒指);        如果(file.isDirectory())
        {
            的文件列表= file.listFiles();
            的for(int i = 0; I&LT; listFile.length;我++)
            {
                f.add(listfile中[I] .getAbsolutePath());
            }
        }
    }
    公共类ViewPagerAdapter扩展PagerAdapter
    {
        //声明变量
        上下文语境;
        的String []排名;
        的String []国家;
        的String []人口;
        的String [] CSWT;
        的String []类;
        INT []标志;
        LayoutInflater吹气;        公共ViewPagerAdapter(上下文的背景下,字符串[]军衔,字符串[]国的String []人口的String [] CSWT,字符串[]类,INT []标志)
        {
            this.context =背景;
            this.rank =排名;
            this.country =国家;
            this.population =人口;
            this.cswt = CSWT;
            this.category =类别;
            this.flag =标志;
        }        @覆盖
        公众诠释getCount将()
        {
            返回refNo.size();
        }        @覆盖
        公共布尔isViewFromObject(查看视图,Object对象)
        {
            返回查看==((RelativeLayout的)对象);
        }        @覆盖
        公共对象instantiateItem(ViewGroup中的容器,INT位置)
        {            //声明变量
            TextView的txtrank;
            TextView的txtcountry;
            TextView的txtpopulation;
            TextView的txtcswt;
            TextView的txtCategoury;
            ImageView的imgflag;            吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            查看ItemView控件= inflater.inflate(R.layout.viewpager_item,集装箱,FALSE);            //定位viewpager_item.xml的TextViews
            txtrank =(TextView中)itemView.findViewById(R.id.rank);
            txtcountry =(TextView中)itemView.findViewById(R.id.country);
            txtpopulation =(TextView中)itemView.findViewById(R.id.population);
            txtcswt =(TextView中)itemView.findViewById(R.id.cswt);
            txtCategoury =(TextView中)itemView.findViewById(R.id.categoury);
            //捕捉位置,并设定在TextViews            txtrank.setText(参考编号: - + refNo.get(位置)的ToString());
            txtcountry.setText(金重: - + GWT.get(位置)的ToString());
            txtpopulation.setText(钻石重量: - + DWT.get(位置)的ToString());
            txtcswt.setText(彩石重量: - + CSWT.get(位置)的ToString());
            txtCategoury.setText(类别: - + Category.get(位置)的ToString());            imgflag =(ImageView的)itemView.findViewById(R.id.flag);            BitmapFactory.Options OP =新BitmapFactory.Options();
            op.inSampleSize = 3;            位图MYBITMAP = BitmapFactory.de codeFILE(f.get(位置),OP);
            imgflag.setImageBitmap(MYBITMAP);            ((ViewPager)容器).addView(ItemView控件);            返回ItemView控件;
        }        @覆盖
        公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象)
        {
            ((ViewPager)容器).removeView((RelativeLayout的)对象);
        }
    }
    //不使用选项菜单在本教程
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
}

请帮我
谢谢


解决方案

使用字符串生成器绑定字符串。

例如:

  StringBuilder的SB =新的StringBuilder();sb.append(15 /);sb.append(08 /);sb.append(2013​​);SYS(sb.tostring());

输出:

  15/08/2013

I want to create slide show of images and information about the images. I am getting images name from json and get the images form sd cards. I want to display slide show like ViewPager. I am parsing the json and store into db.

The problems are

1) how to get images from particular name because I want to display the information about it.

2) I want to implement lazy loading bcoz I have 500+ images and size is 900+kb/images.

below is my json format

[{"Style_RefNo":"SJBG1175","Style_GWT":"5.64","Style_DWT":"0.27","Style_CSWT":"0","ImageName":"SJBG1175.jpg","MainCatname":"BanglesBracelets","MainCatid":"77"}]

below is my code I am successfully implement view pager and images are display normally. but I am facing above 2 problem please help me

public class ViewRing extends Activity 
{

    // Declare Variables
    private ProgressDialog pd;
    public static DataSource dataSource;
    List<GetStyleData> styledata;
    ViewPager viewPager;
    PagerAdapter adapter;
    String[] rank;
    String[] country;
    String[] population;
    String[] cswt;
    String[] category;
    int[] flag;
    File[] listFile;
    Button btnHome;

    ArrayList<String> f = new ArrayList<String>();// list of file paths

    ArrayList<String> refNo = new ArrayList<String>();
    ArrayList<String> GWT = new ArrayList<String>();
    ArrayList<String> DWT = new ArrayList<String>();
    ArrayList<String> CSWT = new ArrayList<String>();
    ArrayList<String> Category = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from viewpager_main.xml
        setContentView(R.layout.viewpager_main);
        dataSource = new DataSource(ViewRing.this);
        pd = new ProgressDialog(ViewRing.this);
        getFromSdcard();
        // Generate sample data

        styledata = dataSource.getRingData();

        for(int i = 0 ; i < styledata.size() ; i++)
        {
            String tempRefNo = styledata.get(i).getStyle_RefNo();
            String tempGWT = styledata.get(i).getStyle_GWT();
            String tempDWT = styledata.get(i).getStyle_DWT();
            String tempCSWT = styledata.get(i).getStyle_CSWT();
            String tempCategory = styledata.get(i).getMainCatname();

            refNo.add(tempRefNo);
            GWT.add(tempGWT);
            DWT.add(tempDWT);
            CSWT.add(tempCSWT);
            Category.add(tempCategory);

            Log.v("RefNo", ""+tempRefNo);
            Log.v("GWT", ""+tempGWT);
            Log.v("DWT", ""+tempDWT);
            Log.v("CSWT", ""+tempCSWT);
            Log.v("Category", ""+tempCategory);
        }


        // Locate the ViewPager in viewpager_main.xml
        viewPager = (ViewPager) findViewById(R.id.pager);

        // Pass results to ViewPagerAdapter Class
        adapter = new ViewPagerAdapter(this, rank, country, population, cswt, category, flag);

        // Binds the Adapter to the ViewPager
        viewPager.setAdapter(adapter);


        btnHome = (Button)findViewById(R.id.btnHome);
        btnHome.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                class RingTask extends AsyncTask<Void, Void, Object>
                {
                    @Override
                    protected void onPreExecute() 
                    {
                        // TODO Auto-generated method stub
                        super.onPreExecute();
                        pd.setTitle("Wait !");
                        pd.setMessage("Loading ....");
                        pd.show();

                    }
                    @Override
                    protected Object doInBackground(Void... params) 
                    {
                        // TODO Auto-generated method stub
                        Intent iRing = new Intent(ViewRing.this, Title.class);
                        startActivity(iRing);
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Object result) 
                    {
                        // TODO Auto-generated method stub
                        super.onPostExecute(result);
                        if(pd.isShowing())
                        {
                            pd.dismiss();
                        }
                    }
                }
                new RingTask().execute();

            }
        });
    }
    public void getFromSdcard()
    {
//      File file= new File(android.os.Environment.getExternalStorageDirectory(),"MapleBear");
        File file = new File("/mnt/sdcard/FinalView/Rings");

        if (file.isDirectory())
        {
            listFile = file.listFiles();
            for (int i = 0; i < listFile.length; i++)
            {
                f.add(listFile[i].getAbsolutePath());
            }
        }
    }
    public class ViewPagerAdapter extends PagerAdapter 
    {
        // Declare Variables
        Context context;
        String[] rank;
        String[] country;
        String[] population;
        String[] cswt;
        String[] category;
        int[] flag;
        LayoutInflater inflater;

        public ViewPagerAdapter(Context context, String[] rank, String[] country, String[] population,String[] cswt,String[] category, int[] flag) 
        {
            this.context = context;
            this.rank = rank;
            this.country = country;
            this.population = population;
            this.cswt = cswt;
            this.category = category;
            this.flag = flag;
        }

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

        @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;
            TextView txtcswt;
            TextView txtCategoury;
            ImageView imgflag;

            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View itemView = inflater.inflate(R.layout.viewpager_item, container, false);

            // Locate the TextViews in viewpager_item.xml
            txtrank = (TextView) itemView.findViewById(R.id.rank);
            txtcountry = (TextView) itemView.findViewById(R.id.country);
            txtpopulation = (TextView) itemView.findViewById(R.id.population);
            txtcswt = (TextView) itemView.findViewById(R.id.cswt);
            txtCategoury = (TextView) itemView.findViewById(R.id.categoury);
            // Capture position and set to the TextViews

            txtrank.setText("Ref. No :- "+refNo.get(position).toString());
            txtcountry.setText("Gold Weight :- "+GWT.get(position).toString());
            txtpopulation.setText("Diamond Weight :- "+DWT.get(position).toString());
            txtcswt.setText("Color Stone Weight :- "+CSWT.get(position).toString());
            txtCategoury.setText("Category :- "+Category.get(position).toString());

            imgflag = (ImageView) itemView.findViewById(R.id.flag);

            BitmapFactory.Options op = new BitmapFactory.Options();
            op.inSampleSize = 3;

            Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position),op);
            imgflag.setImageBitmap(myBitmap);

            ((ViewPager) container).addView(itemView);

            return itemView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) 
        {
            ((ViewPager) container).removeView((RelativeLayout) object);
        }
    }
    // Not using options menu in this tutorial
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

please help me thanks

解决方案

use string builder to bind string..

for example: use

StringBuilder sb=new StringBuilder();

sb.append("15/");

sb.append("08/");

sb.append("2013");

sys(sb.tostring());

output:

15/08/2013

这篇关于如何从JSON和图像从SD卡绑定图片叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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