从列表视图中捕获事件

时间:2011-07-31 22:33:43

标签: android listview

有人可以向我解释点击列表项时会发生什么事件吗?

我有一个自定义listadapter的列表视图。

我实施了

mylistview.setOnItemLongClickListener()

我在单击的视图中操作textview。但我的变化立即迷失了。我想因为在我的监听器被调用之后立即重新绘制了列表。我如何等待列表完成更新然后更改某些内容?

由于

编辑以获取更多信息:

在onCreate()中我有这个:

    lstLog.setOnItemLongClickListener(new android.widget.AdapterView.OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {

            // set edit flag for the selected receipt
            // makes drawing of the list behave accordingly (see ReceiptAdapter)
            edit = position;

            return true;
        }
    });

然后我有一个自定义适配器:

class ReceiptAdapter extends BaseAdapter {
    private LayoutInflater inflater = getLayoutInflater();
    private static final int REGULAR = 0, SELECTED = 1, EDIT = 2;

    public ReceiptAdapter(ReceiptHandler log) {
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);

        // if we didnt receive a recycled view,
        // we need to create a fresh view from scratch
        if (convertView == null) {

            // check what type of receipt we need for this position
            switch (type) {
            case REGULAR:
                // regular receipt
                convertView = inflater.inflate(R.layout.row, parent, false);
                break;
            case SELECTED:
                // selected receipt
                convertView = inflater.inflate(R.layout.row_selected, parent, false);
                // disable edit text field
                convertView.findViewById(R.id.editLogName).setVisibility(View.GONE);
                convertView.findViewById(R.id.txtLogName).setVisibility(View.VISIBLE);
                break;
            case EDIT:
                // selected receipt in edit mode
                convertView = inflater.inflate(R.layout.row_selected, parent, false);
                // enable edit text field
                convertView.findViewById(R.id.txtLogName).setVisibility(View.GONE);
                convertView.findViewById(R.id.editLogName).setVisibility(View.VISIBLE);
                break;
            }
        }

        // set the actual data on our new or recycled view
        Receipt r = getItem(position);

        ((TextView) convertView.findViewById(R.id.txtLogDate)).setText(r.date);
        ((TextView) convertView.findViewById(R.id.txtLogMode)).setText(r.mode);
        ((TextView) convertView.findViewById(R.id.txtLogName)).setText(r.name);

        // these text fields exist only on the full receipt
        if (type == SELECTED || type == EDIT) {
            ((TextView) convertView.findViewById(R.id.txtLogTime)).setText(r.time);
            ((TextView) convertView.findViewById(R.id.txtLogItem1)).setText(r.item1);
            ((TextView) convertView.findViewById(R.id.txtLogValue1)).setText(r.value1);
            ((TextView) convertView.findViewById(R.id.txtLogItem2)).setText(r.item2);
            ((TextView) convertView.findViewById(R.id.txtLogValue2)).setText(r.value2);
        }

        // ship the finished view
        return convertView;
    }

    public int getCount() {
        return log.get().size();
    }

    public Receipt getItem(int position) {
        return log.get().get(position);
    }

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

    @Override
    public int getViewTypeCount() {
        // we know 3 different types:
        // regular receipt, selected receipt, selected receipt in edit mode
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        // return the appropriate viewtype for this position

        if (position == edit)
            return EDIT;

        if (position == lstLog.getCheckedItemPosition())
            return SELECTED;

        else
            return REGULAR;
    }
}

0 个答案:

没有答案