0

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

@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;
4

1 回答 1

0

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

@Override

公共 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;
    }
}
于 2013-06-03T06:43:35.113 回答