如何绑定来自json的图像名称和来自SD卡的图像?

时间:2013-08-05 05:34:07

标签: android json android-viewpager lazy-loading android-sdcard

我想创建图像的幻灯片放映和有关图像的信息。我正在从获取图片名称,并从图像形成SD卡。我想显示像ViewPager这样的幻灯片。我正在解析json并存储到db。

问题是

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

2)我想实现延迟加载 bcoz我有500多张图片,尺寸为900 + kb /图像。

下面是我的json格式

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

下面是我的代码我成功实现了查看寻呼机,图像正常显示。但是我面临着2个问题,请帮帮我

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

请帮帮我 谢谢

1 个答案:

答案 0 :(得分:0)

使用string builder绑定字符串..

例如: 使用

StringBuilder sb=new StringBuilder();

sb.append("15/");

sb.append("08/");

sb.append("2013");

sys(sb.tostring());

<强>输出:

15/08/2013