我有自定义列表视图的文字和图片,问题是第一个项目图像显示错误,剩余的东西显示完美,并在任何时候 我滚动列表视图然后第一个项目图像也显示完美,你能不能给我一些建议我做错了什么
这是我的Asynctask
class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Gropinion_Questions.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
try {
pDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
return Utils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (null == result || result.length() == 0) {
showToast("No data found from web!!!");
Gropinion_Questions.this.finish();
} else {
// get JSON result and give to JsonObject
try {
JSONObject mainJson = new JSONObject(result);
System.out.println("result........."+result);
JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
System.out.println("@@@@@@@@@@ jsonArray @@@@@@@@@@@ "+jsonArray.length());
Singleton.jsonArray = jsonArray;
for (int i = 0; i < 10; i++) {
JSONObject objJson = jsonArray.getJSONObject(i);
objItem = new QuestionsObject();
objItem.setDbcsFacebookProfile(objJson.optString(dbcsFacebookProfile));
objItem.setAvatar_img(objJson.optString(Avatar_image));
objItem.setVotebar(objJson.optString(percentagenumber));
objItem.setProfile_picture_value(objJson.optString(PROFILE_PIC_VALUE));
objItem.setId(objJson.optString(ID));
objItem.setTitle(objJson.optString(TITLE));
objItem.setNick_name(objJson.optString(NICKNAME));
objItem.setQues_type(objJson.optString(QUESTIONTYPE));
objItem.setCategories(objJson.getString(CATEGORIES));
objItem.setSubcatid(objJson.getString(SUBCATID));
objItem.setTotal_vote(objJson.getString(TV));
arrayOfList.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
setAdapterToListview();
if (null != pDialog && pDialog.isShowing()) {
try {
pDialog.dismiss();
pDialog = null;
} catch (Exception e) {
}
}
}
}
}
这是我的适配器
public class QuestionRowAdapter extends ArrayAdapter<QuestionsObject> {
private Activity activity;
private List<QuestionsObject> items;
private QuestionsObject objBean;
private int row;
final Context context = getContext();
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public QuestionRowAdapter(Activity act, int resource,
List<QuestionsObject> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.items = arrayList;
imageLoader = new ImageLoader(activity.getApplicationContext());
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(row, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
objBean = items.get(position);
// converting dp in pixels
holder.pic_image = (ImageView) convertView
.findViewById(R.id.imageView1);
holder.question = (TextView) convertView.findViewById(R.id.textView1);
holder.name = (TextView) convertView.findViewById(R.id.textView2);
holder.percentage_no = (TextView) convertView
.findViewById(R.id.percentagenumber);
holder.answer_img = (ImageView) convertView
.findViewById(R.id.answerimage);
holder.voted = (TextView) convertView.findViewById(R.id.textView6);
holder.voted_now = (TextView) convertView.findViewById(R.id.textView4);
holder.bethefirst = (TextView) convertView.findViewById(R.id.textView5);
holder.percentage_no.setVisibility(View.GONE);
holder.answer_img.setVisibility(View.GONE);
holder.voted.setVisibility(View.GONE);
holder.voted_now.setVisibility(View.GONE);
holder.bethefirst.setVisibility(View.GONE);
Singleton.profile_picture_value = objBean.getProfile_picture_value();
String dbcsFacebookProfile = objBean.getDbcsFacebookProfile();
if (holder.pic_image != null) {
if (Singleton.profile_picture_value.equals("1")) {
String Avatarimageurl = avatarImagePic(objBean.getAvatar_img()
.toString());
imageLoader.DisplayImage(Avatarimageurl, holder.pic_image);
} else {
// for facebook
if (Singleton.login_channel.equals("1")) {
System.out.println("default porfile pic"
+ Singleton.profile_picture_value);
imageLoader.DisplayImage(facebookimagepic(objBean
.getAvatar_img().toString()), holder.pic_image);
}// end of facebook login if
else {
holder.pic_image.setImageResource(R.drawable.app_logooo); //
}
}
}
if (Singleton.profile_picture_value.equals("0")
&& dbcsFacebookProfile.equals("0")) {
System.out.println("default porfile pic in else"
+ Singleton.profile_picture_value);
holder.pic_image.setImageResource(R.drawable.app_logooo);
}
/*
* if (holder.pic_image != null && null != objBean.getBitmap()) {
* holder.pic_image.setImageBitmap(objBean.getBitmap()); }
*/
if (holder.question != null && null != objBean.getTitle()
&& objBean.getTitle().trim().length() > 0) {
holder.question.setText(Html.fromHtml(objBean.getTitle()));
}
if (holder.name != null && null != objBean.getNick_name()
&& objBean.getNick_name().trim().length() > 0) {
holder.name.setText(Html.fromHtml(objBean.getNick_name()));
}
String totalVote = objBean.getTotal_vote();
if (totalVote.equals("0")) {
holder.percentage_no.setVisibility(View.INVISIBLE);
holder.answer_img.setVisibility(View.INVISIBLE);
holder.voted.setVisibility(View.INVISIBLE);
holder.voted_now.setVisibility(View.INVISIBLE);
holder.bethefirst.setVisibility(View.VISIBLE);
holder.bethefirst.setText("Be the 1st\nto vote!");
}// End if block
else {
String ques_Type = objBean.getQues_type();
String newvoteBar = objBean.getVotebar();
String[] newVoteArray = newvoteBar.split(",");
if (ques_Type.equals("0")) {
if (holder.percentage_no != null
&& null != objBean.getVotebar()
&& objBean.getVotebar().trim().length() > 0) {
holder.percentage_no.setVisibility(View.VISIBLE);
holder.answer_img.setVisibility(View.VISIBLE);
holder.voted.setVisibility(View.VISIBLE);
holder.voted_now.setVisibility(View.VISIBLE);
String votePercente = newVoteArray[0]; // to store the vote
// percentage values
int size = votePercente.length();
String votedImageName = newVoteArray[1]; // to store the//
// images
votedImageName.replaceAll("\\s", "");
holder.percentage_no.setText(votePercente + "%");
if (votedImageName.equals("Yes")
&& holder.answer_img != null) {
holder.answer_img.setImageResource(R.drawable.yesimg);
} else {
holder.answer_img.setImageResource(R.drawable.noimg);
}
}
}// End if block question tye
else {
String votePercente = newVoteArray[0];
try {
String imageString = newVoteArray[2];
imageString = imageString.replaceAll(" ", "");
String AnswerImageurl = answerImage(imageString);
imageLoader.DisplayImage(AnswerImageurl, holder.answer_img);
holder.percentage_no.setVisibility(View.VISIBLE);
holder.answer_img.setVisibility(View.VISIBLE);
holder.voted.setVisibility(View.VISIBLE);
holder.voted_now.setVisibility(View.VISIBLE);
holder.percentage_no.setText(votePercente + "%");
} catch (Exception e) {
e.printStackTrace();
}
}// End else block Multi choice question
}
Animation animation = null;
animation = AnimationUtils.loadAnimation(context,
R.anim.slidetop_tobottom);
animation.setDuration(500);
convertView.startAnimation(animation);
animation = null;
return convertView;
}
// get left hand side image based on the avatar
public String avatarImagePic(String pic) {
String url = MainURL.address
+ "/app3/modules/boonex/photos/data/avatar_75_75/" + pic;
return url;
}
// get right hand side image based on the product
public String answerImage(String img) {
String url = MainURL.address
+ "/app3/modules/boonex/photos/data/thumb_75_75/" + img;
return url;
}
public class ViewHolder {
public TextView question, name;
public ImageView pic_image, answer_img;
public ProgressBar progressbar;
public RatingBar rat;
public TextView voted_now, percentage_no, voted, bethefirst;
}
public String facebookimagepic(String pic) {
String url = "http://graph.facebook.com/" + pic + "/picture?type=large";
return url;
}
// get facebook profile pic based on the user id
public Bitmap fbimagepic(String pic) {
Bitmap bit2 = null;
try {
URL feedImage2 = new URL("http://graph.facebook.com/" + pic
+ "/picture?type=large");
HttpURLConnection conn2 = (HttpURLConnection) feedImage2
.openConnection();
InputStream is2 = conn2.getInputStream();
bit2 = BitmapFactory.decodeStream(is2);
} catch (Exception e) {
}
return bit2;
}
}