大家好我是新來的android。我正在處理聊天應用程序,現在我遇到了自定義適配器的問題。它的工作與文本聊天完美,但是當我在listview中加載圖像時出現問題。問題是,當我滾動聊天2-3次圖像覆蓋文本。我檢查我的所有代碼與日誌和它顯示只有一個時間圖像實現特定的位置,但在列表視圖隨機顯示圖像。我已經嘗試更多的谷歌和相關的問題,但沒有任何幫助。Android自定義適配器無法正常工作的聊天列表視圖
這裏當我加載第一次chat..its顯示
,當我在每一個文字聊天有時會滾動顯示的圖像。
誰能幫助me.plz.Thanks。
這裏是我的適配器:
public class ChatMainAdapter extends BaseAdapter {
private static final int TYPE_ITEM_ME = 0;
private static final int TYPE_ITEM_OTHER = 1;
private Context context;
private ArrayList <ChatMessageLocalDBModel> arrayList;
private static String currentUserObjectId;
private Bitmap myBimap, UserBitmap;
public ChatMainAdapter(Context context, ArrayList <ChatMessageLocalDBModel> arrayList, String currentUserObjectId, Bitmap userBitmap, Bitmap myBimap) {
this.context = context;
this.arrayList = arrayList;
this.currentUserObjectId = currentUserObjectId;
this.UserBitmap = userBitmap;
this.myBimap = myBimap;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
String isMe = arrayList.get(position).getFrom();
return isMe.equalsIgnoreCase(currentUserObjectId) ? TYPE_ITEM_ME : TYPE_ITEM_OTHER;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final int type;
type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM_ME:
{
convertView = LayoutInflater.from(context).inflate(
R.layout.chat_listview_item_me, null);
holder.imgViewUserPic = (ImageView) convertView.findViewById(R.id.chat_item_ivProfileMe);
holder.body = (TextView) convertView.findViewById(R.id.chat_item_tv_me_message);
holder.time = (TextView) convertView.findViewById(R.id.chat_item_tv_me_time);
holder.llyPic = (LinearLayout) convertView.findViewById(R.id.chat_lly_image);
holder.llyPic.setBackgroundResource(0);
holder.body.setTextIsSelectable(true);
}
break;
case TYPE_ITEM_OTHER:
{
convertView = LayoutInflater.from(context).inflate(
R.layout.chat_listview_item_other, null);
holder.imgViewUserPic = (ImageView) convertView.findViewById(R.id.chat_item_ivProfileOther);
holder.body = (TextView) convertView.findViewById(R.id.chat_item_tv_other_message);
holder.time = (TextView) convertView.findViewById(R.id.chat_item_tv_other_time);
holder.body.setTextIsSelectable(true);
}
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Log.i("NPath", "" + "Pos:" + position + " :- " + arrayList.get(position).getPath());
if (arrayList.get(position).getPath().equalsIgnoreCase("NO IMAGE")) {
holder.body.setText(arrayList.get(position).getMessage());
holder.time.setText(arrayList.get(position).getTime());
Log.i("NPath", "pos:" + position + "" + "is text and is : " + arrayList.get(position).getMessage() + "" + type);
} else {
Log.i("NPath", "pos:" + position + "" + "is image:" + type);
holder.body.setVisibility(View.GONE);
holder.time.setVisibility(View.GONE);
File path = new File("" + arrayList.get(position).getPath());
if (path.exists()) {
Bitmap mBitmap = BitmapFactory.decodeFile(arrayList.get(position).getPath());
final BitmapDrawable background = new BitmapDrawable(mBitmap);
holder.llyPic.setVisibility(View.VISIBLE);
holder.llyPic.setBackgroundDrawable(background);
} else {
convertView.setVisibility(View.GONE);
Log.e("NFILENOEXICST", "No file exist");
}
}
if (type == TYPE_ITEM_ME) {
holder.imgViewUserPic.setImageBitmap(myBimap);
} else {
holder.imgViewUserPic.setImageBitmap(UserBitmap);
}
final ViewHolder finalHolder = holder;
holder.body.setOnLongClickListener(new View.OnLongClickListener() {@Override
public boolean onLongClick(View v) {
ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(finalHolder.body.getText());
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
return false;
}
});
return convertView;
}
final static class ViewHolder {
public ImageView imgViewUserPic;
public TextView body;
public TextView time;
public LinearLayout llyPic;
}
}
這裏是我的佈局http://pastebin.com/6xSqGpKC
無用的問題..郵編 – Gattsu
有在你的適配器的問題,請張貼代碼也 –
@WillTorres我已更新我的代碼 – Nils