Android Listview混淆了项目

时间:2015-04-04 11:07:13

标签: android android-layout listview android-listview

我有一个带有自定义项目的ListView。

项目的布局:

我有以下用于存储信息的类:

public class MainItem{

private int DrawableId;
private String title;

public  MainItem(String title, int drawable)
{
    this.title = title;
    this.DrawableId = drawable;
}

public String getTitle() {
    return title;
}

public int getDrawableId() {
    return DrawableId;
}}

public class MainRow{

public MainItem getMainItem1() {
    return mainItem1;
}

public MainItem getMainItem2() {
    return mainItem2;
}

public MainItem getMainItem3() {
    return mainItem3;
}

私有MainItem mainItem1,mainItem2,mainItem3;

public int getNumberOfItems() {
    return numberOfItems;
}

private int numberOfItems = 0;

public MainRow(MainItem mainItem)
{
    this(1,mainItem,null,null);
}

public MainRow(MainItem mainItem, MainItem mitem2)
{
    this(2,mainItem,mitem2,null);
}
public MainRow(int nb, MainItem mainItem, MainItem mainItem2, MainItem mainItem3)
{
    this.mainItem1 = mainItem;
    this.mainItem2 = mainItem2;
    this.mainItem3 = mainItem3;
    this.numberOfItems = nb;
}}

并且我的BaseAdapter是:

public class MainItemAdapter extends BaseAdapter {

List<MainRow> mitems;

MainScreenItemLisener mainScreenItemLisener;

    public MainItemAdapter(MainScreenItemLisener mainScreenItemLisener, ArrayList<MainRow> list)
    {
        mitems = list;
        this.mainScreenItemLisener = mainScreenItemLisener;
    }

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

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

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

static class ViewHolder {
    ImageView imgLeft, imgRight, imgMiddle;
    TextView tvLeft, tvRight, tvMiddle;
    LinearLayout mLeft, mRight, mMiddle;
}

@Override
public boolean isEnabled(int position) {
    return false;
}

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


    if (convertView == null)
    {
        holder = new ViewHolder();

        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.main_screen_two, null);
           holder.imgLeft = (ImageView) convertView.findViewById(R.id.image_left);
        holder.imgMiddle = (ImageView) convertView.findViewById(R.id.image_middle);
            holder.imgRight = (ImageView) convertView.findViewById(R.id.image_right);
            holder.tvLeft = (TextView) convertView.findViewById(R.id.tvLeft);
            holder.tvRight = (TextView) convertView.findViewById(R.id.tvRight);
             holder.tvMiddle = (TextView) convertView.findViewById(R.id.tvMiddle);
            holder.mLeft = (LinearLayout) convertView.findViewById(R.id.mLinearLauoutleft);
            holder.mRight = (LinearLayout) convertView.findViewById(R.id.mLinearLauoutRight);
        holder.mMiddle = (LinearLayout) convertView.findViewById(R.id.mLinearLauoutMiddle);
        convertView.setTag(holder);

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

    final MainRow mItem = mitems.get(position);

    if(mItem.getNumberOfItems() >=1)
    if(mItem.getMainItem1() != null)
    {

        Picasso.with(parent.getContext())
                  .load(mItem.getMainItem1().getDrawableId())
                    .into( holder.imgLeft);
            holder.tvLeft.setText(mItem.getMainItem1().getTitle());

          holder.mLeft.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
             mainScreenItemLisener.ItemClicked(mItem.getMainItem1());     }
          });

    }

    if(mItem.getNumberOfItems() ==1) return convertView;

    if(mItem.getNumberOfItems() >=2)
    if(mItem.getMainItem2() != null)
    {

        Picasso.with(parent.getContext())
                .load(mItem.getMainItem2().getDrawableId())
                .into( holder.imgMiddle);
        holder.tvMiddle.setText(mItem.getMainItem2().getTitle());

        holder.mMiddle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainScreenItemLisener.ItemClicked(mItem.getMainItem2());}
        });
    }

    if(mItem.getNumberOfItems() >=3)
    if(mItem.getMainItem3() != null)
    {

        Picasso.with(parent.getContext())
                .load(mItem.getMainItem3().getDrawableId())
                .into( holder.imgRight);
        holder.tvRight.setText(mItem.getMainItem3().getTitle());

        holder.mRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mainScreenItemLisener.ItemClicked(mItem.getMainItem3());}
        });
    }
    return convertView;
}}

我的问题如下:

首次创建列表视图时,如下图所示:

设置:

items.add(new MainRow(3, new MainItem(getString(R.string.evenimente),R.drawable.evenimente),
                            new MainItem(getString(R.string.atracti_turistice),R.drawable.atracti_turistice),
                            new MainItem(getString(R.string.ture_turistice), R.drawable.ghid)));

    items.add(new MainRow(3, new MainItem(getString(R.string.restaurante),R.drawable.restaurant),
                              new MainItem(getString(R.string.hoteluri),R.drawable.hotel),
                               new MainItem(getString(R.string.spa), R.drawable.spa_wellnes)));

    items.add(new MainRow(3, new MainItem(getString(R.string.nightlife),R.drawable.clubs),
                              new MainItem(getString(R.string.shopping),R.drawable.shop),
                              new MainItem(getString(R.string.transport_local), R.drawable.transport)));


    items.add(new MainRow(3, new MainItem(getString(R.string.parcari),R.drawable.parcari),
                               new MainItem(getString(R.string.servicii_medicale),R.drawable.service_medicale),
                            new MainItem(getString(R.string.service_auto), R.drawable.service_auto)));


    items.add(new MainRow(new MainItem(getString(R.string.map),R.drawable.harta)));

    mainItemAdapter = new MainItemAdapter(this,items);

    mListView.setAdapter(mainItemAdapter);

在最后一行只有&#34; Altele&#34;东西被打了,但是当第一次显示列表视图时,就像你看到另外一个项目中的东西一样。怎么办?

This is how it looks like!

1 个答案:

答案 0 :(得分:0)

你错过了&#34;否则&#34; if逻辑中的case。您必须显式设置每一行,因为正在重复使用这些行。此外,没有花括号的if-logic是糟糕的mojo,并且难以理解你的getView()。

例如,如果您的最后一行没有3项,该怎么办?以下2个插槽将重新使用并显示之前在这些行中的图像。这一行if(mItem.getNumberOfItems() ==1) return convertView; 是你的罪魁祸首。

现在......你的代码(已更新):

if(mItem.getNumberOfItems() >=2) {
    if(mItem.getMainItem2() != null) {
        // do something
    }
}

// OR

if(mItem.getNumberOfItems() >=2 && mItem.getMainItem2() != null) {
        // do something
}