0
我在我的mysql中有一篇文章,每篇文章都有一个图像。如何在async伺服器onPostExecute上从服务器的imageview上显示图像?
**article_id** **article_image**
00001 display/Article1image.png
00002 display/Article2image.jpg
00003 display/Article3image.jpg
00004 display/Article4image.png
让我们假设我打印上的LinearLayout的所有商品信息与像article_content,article_writer不同的看法,date_published等..和我想显示对编程产生的article_image ImageView
假设`“www.sampleweb.com /”+ _ jsonObject.optString(“article_image”)。toString()'是服务器映像的完整链接。
这是我的完整代码,我如何创建imageview与图像的上述url的内容?
@Override
public void onPostExecute(String result) {
//headlinehead.setText(result);
try{
JSONObject jsonRootObj = new JSONObject(result);
JSONArray jsonArray = jsonRootObj.optJSONArray("Data");
JSONObject jsonObject = jsonArray.getJSONObject(0);
//code for headline
this.headlinehead.setText(jsonObject.optString("title").toString());
this.shortdesc.setText(jsonObject.optString("shortdesc").toString());
this.headlinesender.setText(jsonObject.optString("penname").toString());
this.img.setTag(jsonObject.optString("id").toString());
//code for headline
for (int p = 1; p < jsonArray.length(); p++){
JSONObject _jsonObject = jsonArray.getJSONObject(p);
LinearLayout conteach = new LinearLayout(getActivity().getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
conteach.setLayoutParams(lp);
conteach.setOrientation(LinearLayout.VERTICAL);
TextView art_title = new TextView(getActivity().getApplicationContext());
art_title.setText(_jsonObject.optString("title").toString());
art_title.setTextSize(20);
art_title.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
TextView art_shortdesc = new TextView(getActivity().getApplicationContext());
art_shortdesc.setText(_jsonObject.optString("shortdesc").toString());
art_shortdesc.setTextSize(14);
art_shortdesc.setTypeface(null, Typeface.ITALIC);
art_shortdesc.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
TextView art_sender = new TextView(getActivity().getApplicationContext());
art_sender.setText("Written by: " + _jsonObject.optString("sender").toString());
art_sender.setTextSize(14);
art_sender.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
art_sender.setPadding(0, 0, 0, 15);
conteach.addView(art_title);
conteach.addView(art_shortdesc);
conteach.addView(art_sender);
conteach.setTag(_jsonObject.optString("id").toString());
conteach.setClickable(true);
conteach.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SpecificArticleFragment spcf = new SpecificArticleFragment();
Bundle args = new Bundle();
args.putString("art_id", v.getTag().toString());
spcf.setArguments(args);
FragmentManager ft = getFragmentManager();
ft.beginTransaction().replace(R.id.content_frame, spcf).commit();
}
});
this.llist.addView(conteach);
}
this.pbColBul.setVisibility(ProgressBar.GONE);
} catch(JSONException e){
}
}
我会建议你使用许多现有的库之一。看看这两个:毕加索http://square.github.io/picasso/和Glide https://github.com/bumptech/glide/。否则,你将不得不下载并可能自己缓存图像。 –