2013-08-05 31 views
1

我想创建图像幻灯片放映和图像信息。我得到图像名称从json并获得图像形式的SD卡。我想要显示像ViewPager幻灯片。我正在解析JSON并将其存储到数据库中。如何绑定json的图像名称和sd卡的图像?

的问题是

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

2)我想实现延迟加载 bcoz我有500多个图像和大小是900 + kb/images。下面

是我的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; 
    } 
} 

请 感谢

+0

对于语法错误和拼写错误感到抱歉。英语不是我的母语,我很穷,但我会尽力纠正它。 –

+0

谢谢。你能帮我解决问题吗?如果你有答案。 –

回答

0

使用string builder绑定字符串帮助我..

例如: 使用

StringBuilder sb=new StringBuilder(); 

sb.append("15/"); 

sb.append("08/"); 

sb.append("2013"); 

sys(sb.tostring()); 

输出:

15/08/2013