Android 如何从json绑定图像名称和从sd卡绑定图像?
我想创建图像和图像信息的幻灯片。我从json获取图像名称,并从sd卡获取图像。我想像ViewPager一样显示幻灯片放映。我正在解析json并将其存储到db中 问题是 1) 如何从特定名称获取图像,因为我想显示有关该名称的信息 2) 我想实现延迟加载bcoz我有500多个图像,大小为900+kb/图像 下面是我的json格式Android 如何从json绑定图像名称和从sd卡绑定图像?,android,json,android-viewpager,lazy-loading,android-sdcard,Android,Json,Android Viewpager,Lazy Loading,Android Sdcard,我想创建图像和图像信息的幻灯片。我从json获取图像名称,并从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","Ima
[{"Style_RefNo":"SJBG1175","Style_GWT":"5.64","Style_DWT":"0.27","Style_CSWT":"0","ImageName":"SJBG1175.jpg","MainCatname":"BanglesBracelets","MainCatid":"77"}]
下面是我的代码,我成功地实现了查看寻呼机和图像显示正常。但我面临以上两个问题,请帮助我
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;
}
}
public class ViewRing扩展活动
{
//声明变量
私营部门;
公共静态数据源;
列表样式数据;
查看页面查看页面;
PagerAdapter适配器;
字符串[]秩;
字符串[]国家;
字符串[]总体;
字符串[]cswt;
字符串[]类别;
int[]标志;
文件[]列表文件;
按钮btnHome;
ArrayList f=new ArrayList();//文件路径列表
ArrayList refNo=新的ArrayList();
ArrayList GWT=新的ArrayList();
ArrayList DWT=新的ArrayList();
ArrayList CSWT=新的ArrayList();
ArrayList类别=新建ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//从viewpager_main.xml获取视图
setContentView(R.layout.viewpager_main);
dataSource=新数据源(ViewRing.this);
pd=新建进度对话框(ViewRing.this);
getFromSdcard();
//生成样本数据
styledata=dataSource.getRingData();
对于(int i=0;iStringBuilder sb=new StringBuilder();
sb.append("15/");
sb.append("08/");
sb.append("2013");
sys(sb.tostring());
15/08/2013