用于android的自定义listview适配器 - 如何在getView()中获得正确的视图?

时间:2013-01-15 21:33:13

标签: android android-layout android-adapter

我正在尝试编写自定义适配器以将简单类调整为ListView。该类用于SMS消息,它包含简单字段,如正文,发件人地址等。下面,请参阅我的适配器布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <ImageView
    android:id="@+id/imgContactPhoto"
    android:contentDescription="Contact photo"
    android:layout_width="90sp"
    android:layout_height="90sp" />

    <TextView
    android:id="@+id/lblMsg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/imgContactPhoto" 
    android:paddingLeft="10sp" />

</RelativeLayout>

我的自定义适配器类:

public class SmsListAdapter extends ArrayAdapter{

  private int resource;
  private LayoutInflater inflater;
  private Context context;

  @SuppressWarnings("unchecked")
  public SmsListAdapter(Context ctx, int resourceId, List objects) {
      super(ctx, resourceId, objects);
      resource = resourceId;
      inflater = LayoutInflater.from( ctx );
      context=ctx;
  }

  @Override
  public View getView (int position, View convertView, ViewGroup parent) {
        //create a new view of the layout and inflate it in the row
        convertView = ( RelativeLayout ) inflater.inflate( resource, null );

        // Extract the object to show 
        Sms msg = (Sms) getItem(position);

        // Take the TextView from layout and set the message
        TextView lblMsg = (TextView) convertView.findViewById(R.id.lblMsg);
        lblMsg.setText(msg.getBody());

        //Take the ImageView from layout and set the contact image
        long contactId = fetchContactId(msg.getSenderNum());
        String uriContactImg = getPhotoUri(contactId).toString();
        ImageView imgContactPhoto = (ImageView) convertView.findViewById(R.id.imgContactPhoto);
        int imageResource = context.getResources().getIdentifier(uriContactImg, null, context.getPackageName());
        Drawable image = context.getResources().getDrawable(imageResource);
        imgContactPhoto.setImageDrawable(image);
        return convertView;
  }
}

当我尝试激活适配器时,我在getView()的第一行收到错误,说明TextView无法强制转换为RelativeLayout。

我不清楚的是为什么它首先是TextView。我的列表项布局设置为RelativeLayout,这是应该被夸大的内容,除非我弄错了。任何人都可以帮我调试吗?

3 个答案:

答案 0 :(得分:3)

在这一行:

       convertView = ( RelativeLayout ) inflater.inflate( resource, null );

而不是资源,请尝试使用布局的完整ID-R.layout.whatever

此外,如果传入视图为空,则只应对布局进行膨胀。否则视图已经膨胀,你应该只覆盖它的所有值。

答案 1 :(得分:2)

使用BaseAdapter创建自定义适配器。从一开始就要管理起来有点困难,但以后会让你的工作变得更轻松。

public class GeneralAdapter extends BaseAdapter{

private LayoutInflater mInflater    = null;
private ArrayList<String> info      = null;

public GeneralAdapter( ArrayList<String> info ) {
    this.info = info;
}

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

@Override
public Object getItem(int position) {
    return info.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.YOURLAYOUT, null);

        holder                  = new ViewHolder();
        holder.generalTV        = (TextView) convertView.findViewById(R.id.lblMsg);
        holder.generalIV        = (ImageView) convertView.findViewById(R.id.imgContactPhoto);

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

    holder.generalTV.setText(info.getBody());
    holder.generalIV.setBackgroundResource(R.id.YOURIMAGE);

    return null;
}

private class ViewHolder {
    TextView generalTV;
    ImageView generalIV;
}

}

答案 2 :(得分:1)

删除convertViewRelativeLayout的显式广播应该会有所帮助。