如何使用三种不同的行布局制作ListView?

时间:2013-06-03 06:19:10

标签: android listview layout inflate

我正在制作一个listView,我想要扩展三种不同的布局。每行示例都有一个条件:如果x ==“F”则R.layout.row1应该膨胀,如果x ==“t”R.layout.row2应该膨胀,我有三行..我尝试使用

@Override
public int getViewTypeCount() {
    return 3;
}
//
@Override
public int getItemViewType(int position) {

    objBean = items.get(position);
    if (objBean.gettype() == "I") {
        return layout2;
    } else if (objBean.gettype() == "F") {
        return layout1;
    } else {
        return layout3;
    }
    return i;

}

和getview方法

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    final ViewHolder holder;
    objBean = items.get(position);

    int type = getItemViewType(position);

    String t = objBean.gettype();
    // if(i == 0){

    if (view == null) {

        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        if (type == layout2) {

            view = inflater.inflate(R.layout.row1, null);

        } else if (type == layout1) {

            view = inflater.inflate(R.layout.list, null);

        } else{
            view = inflater.inflate(R.layout.twitter, null);
        }
        holder = new ViewHolder();
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

,其中

    int layout1 = 0;
int layout2 = 1;
int layout3 = 2;

1 个答案:

答案 0 :(得分:0)

得到答案,问题出在if语句中,它没有执行。 我改变了

@Override

public int getItemViewType(int position){

objBean = items.get(position);
if (objBean.gettype() == "I") {
    return layout2;
} else if (objBean.gettype() == "F") {
    return layout1;
} else {
    return layout3;
}
return i;

}

@Override
public int getItemViewType(int position) {

    objBean = items.get(position);
    String check = objBean.gettype().toString();
    int k = objBean.getflag();
    if (check.equals("F")) {
        return layout1;
    } else if (check.equals("T")) {
        return layout3;
    } else  {
        return layout2;
    }
}