Android列表视图行更改值

时间:2013-06-21 23:48:30

标签: android listview android-arrayadapter

我有一个包含3种元素的ListView。行和QueueRows是一组有点复杂的子控件,而HeaderRow只包含textView。 我做的是添加HeaderRow,少数QueueRows,headerRow和几行。 QueueRow需要每秒更新一次。这就是为什么我编写了一个在适配器上调用notifyDataSetChanged的代码。它每秒更新一次queuerow,但也有一个问题 - 我的两个标题切换位置半秒钟。 在每0.5秒后他们交换。你对如何预防它们有什么想法吗?

那是我的代码:

class BuildingsAdapter extends ArrayAdapter<BaseRow> {

private static Bitmap SOURCE_BITMAP;

private MainActivity mainActivity_;

private Handler handler_;
private Runnable runnable_;

public BuildingsAdapter(MainActivity mainActivity) {

    super(mainActivity, R.layout.row);
    this.mainActivity_ = mainActivity;
    SOURCE_BITMAP = BitmapFactory.decodeResource(
            mainActivity_.getResources(), R.drawable.images);
    handler_ = new Handler();
    runnable_ = new Runnable() {

        @Override
        public void run() {

            notifyDataSetChanged();
            if (hasQueueItems())
                handler_.postDelayed(runnable_, 1000);
        }
    };
}

@Override
public void add(BaseRow object) {

    if (!hasQueueItems() && object.getClass().equals(QueueRow.class))
        handler_.postDelayed(runnable_, 1000);
    super.add(object);
}

private boolean hasQueueItems() {

    for (int i = 0; i < getCount(); ++i) {
        if (getItem(i).getClass().equals(QueueRow.class))
            return true;
    }

    return false;
}

@Override
public int getItemViewType(int position) {

    return getItem(position).getType();
}

@Override
public int getViewTypeCount() {

    return 3;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    int type = getItemViewType(position);

    View row = convertView;

    switch (type) {
    case BaseRow.QUEUE_ROW: {
        QueueRowViewHolder holder = new QueueRowViewHolder();
        //whatever
                    return convertView;
    }
    case BaseRow.ROW: {
        RowViewHolder holder = new RowViewHolder();
        //whatever
                    return convertView;
    }
    case BaseRow.HEADER_ROW: {
        HeaderViewHolder holder = new HeaderViewHolder();
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.header_row, parent, false);

            HeaderRow headerRow = (HeaderRow) getItem(position);

            holder.nameTextView_ = (TextView) row
                    .findViewById(R.id.categoryNameTextView);
            holder.nameTextView_.setText(headerRow.getName());

            row.setTag(holder);
        } else {
            holder = (HeaderViewHolder) row.getTag();
        }
        return row;
    }
    }
    return null;
}

static class QueueRowViewHolder {

    ImageView qItemImageView_;
    TextView qItemNameTextView_;
    ProgressBar qProgressBar_;
    TextView qLevelTextView_;
    TextView qTimeTextView_;
    int position_;
}

static class RowViewHolder {

    ImageView itemImageView_;
    TextView nameTextView_;
    TextView levelTextView_;
    TextView woodTextView_;
    TextView ironTextView_;
    TextView stoneTextView_;
    TextView goldTextView_;
    Button actionButton_;
    TextView timeTextView_;
}

static class HeaderViewHolder {

    TextView nameTextView_;
}

1 个答案:

答案 0 :(得分:0)

确定!我发现了 - 我没有设置HeaderRow的值;)