如何根据需要准备适配器以显示ImageView而不是TextView

时间:2014-07-05 15:12:11

标签: android listview android-listview adapter

我有一个ListView用于我的聊天应用程序,有些时候我需要删除TextView并加载图像,具体取决于数据输入,为了实现我有一些想法,我已经尝试了一个,但listview Messes Up,当我添加图像,任何方式,想到可能可以使用一些经验从一些好人请问有什么是正确的方法这样做?他们认为的方式是这些:

1.创建2个ViewHolders和ConvertViews设置,每次加载1套

2.在listView中将imageView视为不可见,并在需要时可见(这是我试过但失败了就像说的那样)

如果你能给我一个小费或帮助我真的很感激,谢谢Alot

我已经尝试过的一部分:

     @Override
 public int getCount() {
return mCursor.getCount();
}

@Override
public Object getItem(int position) {
return mCursor.getString(position);
}

@Override
public long getItemId(int position) {
return mCursor.getPosition();
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
    holder = new ViewHolder();
    convertView = mLayoutInflater.inflate(R.layout.listitem_discuss, null);
    this.convertview=convertView;



System.out.println("ConverView null");

holder.PimageView = (ImageView)convertView.findViewById(R.id.PimageView);
//PimageView is the One i have Problem With, it gets Visible When Inputs are Images
holder.wrapper = (LinearLayout) convertView.findViewById(R.id.wrapper);
//wrapper is my LinearLayout child of FrameLayout
holder.theMessage = (TextView) convertView.findViewById(R.id.comment);
holder.theName = (TextView) convertView.findViewById(R.id.MSGname);
holder.theImage = (ImageView)convertView.findViewById(R.id.MSGimage);
holder.lp = (FrameLayout.LayoutParams) holder.theName.getLayoutParams();
holder.paramsleft = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,Gravity.LEFT);
holder.paramsright = new   FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,Gravity.RIGHT);

convertView.setTag(holder);

}else {
   holder = (ViewHolder) convertView.getTag();
}

mCursor.moveToPosition(position);   
String imagenamer= 
(mCursor.getString(mCursor.getColumnIndex("username")).split("\\@"))[0];
int isright= Integer.valueOf(mCursor.getString(mCursor.getColumnIndex("isright")));



if(!"".equals(mCursor.getString(mCursor.getColumnIndex("isfile")))){
 System.out.println("its a file");
String type=(mCursor.getString(mCursor.getColumnIndex("isfile")).split("\\@ADAT")
[0]);
System.out.println("Type:"+type);
switch (type) {
case "image":
        //theMessage is replaced with PimageView and so TheView
        holder.theMessage.setVisibility(View.GONE);
        holder.PimageView.setVisibility(View.VISIBLE);  
        System.out.println("its an image");
        holder.PimageView.setLayoutParams(new   
LinearLayout.LayoutParams(PViewWidth,LayoutParams.WRAP_CONTENT));
if(isright==0){
holder.PimageView.setBackgroundResource(R.drawable.bubble_yellow);

}else{
holder.PimageView.setBackgroundResource(R.drawable.bubble_yellow);

 }

但ListView会在滚动时弄乱位置

1 个答案:

答案 0 :(得分:0)

您有两种选择。一个是走可见/不可见的路线。如果你走这条路,你必须在每次调用getView()时使正确的视图可见和不可见,并且你的getView需要知道要使用哪一个。

选项二是基于适配器的完全自定义适配器。具体来说,除了普通函数之外,您还需要重载getViewTypeCount()和getItemViewType()。对于这样一个简单的视图,我可能会选择路径1,对于复杂的视图,此选项更好。