android中的Databind Complex列表

时间:2012-11-09 03:42:59

标签: android list listview layout

Mockup 所以我想在下面创建这个UI。这是一个大学课程,我没有多少时间。我有一切工作,但我无法围绕制作事件列表。我有一个java类eventList,用数据库中的数据填充List。有什么建议?我是否必须动态添加2个按钮(向上/向下)和足够的文本视图来提供数据?如果我这样做,我将如何保持格式?我应该把它全部放在主页的滚动视图中吗?

1 个答案:

答案 0 :(得分:1)

使用自定义适配器将其设为ListView。每行可以设计为RelativeLayout,因此您可以将事件名称,位置等放在那里。

适配器看起来像这样:(代码消毒)

public class POPublicationAdapter extends ArrayAdapter<Wrap<POUserPublication>> {
    private final Context context;
    ... 
    private OnClickListener faveClick;
    private OnClickListener shortClick;
    ...

    public POPublicationAdapter( Context context, List<WrapPO<POUserPublication>> values, 
            OnClickListener faveClick, OnClickListener shortClick, ... ) {
        super(context, R.layout.row_publication, values);
        this.context = context;
        this.listId = listId;
        this.faveClick = faveClick;
        this.shortClick = shortClick;
            ...
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        WrapPO<POUserPublication> item = getItem(position);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = null;

        if( item.item != null ){
            rowView = inflater.inflate(R.layout.row_publication, parent, false);
            rowView.setTag( item );
            TextView tvLabel = (TextView) rowView.findViewById(R.id....);
            ImageView ivPubFavorite = (ImageView) rowView.findViewById(R.id....);

            TextView tvSubLabel = (TextView) rowView.findViewById(R.id.publicationSublabel);
            tvLabel.setText( item.item.pub().get( PO.displayName ) );
            if( !item.item.pub().existsDefault() ) {

            }
            if( listId == POLayer.LIST_REQUIRED ) 
                tvSubLabel.setText(String.format("(Due on %1$TD)", item.item.asDate( PO.dueDate )));
            else if( listId == POLayer.LIST_PREVIEW )
                tvSubLabel.setText( String.format( "(Preview until %1$TD)", item.item.pub().asDate( PO.distributionDate ) ) );
            else
                tvSubLabel.setText( item.item.getTouchInfo() );

            if( this.listId == POLayer.LIST_FAVE ){
                setOfflineImage(ivPubFavorite, SG.isOnline, item.item.isFavorite() ? R.drawable.ls_star_remove : R.drawable.ls_star_grey );
            } else {
                setOfflineImage(ivPubFavorite, SG.isOnline, item.item.isFavorite() ? R.drawable.ls_star_gold : R.drawable.ls_star_grey );
            }
            ivPubFavorite.setClickable(true);
            ivPubFavorite.setTag(item);
            ivPubFavorite.setOnClickListener( faveClick );

            ivNot.setClickable( false );
            if( listId == POLayer.LIST_REQUIRED || listId == POLayer.LIST_UNREAD ) {
                ivNot.setTag(item);
                ivNot.setOnClickListener( notClick );
                if( item.item.isRequired() ) {
                    if( item.item.isComplete() ) {
                        setOfflineImage( ivNot, item.item.asString( PO.offlineUTC ).isEmpty(), R.drawable.ls_doc_confirmed );
                    } else {
                        ivNot.setClickable(true);
                        ivNot.setImageResource( R.drawable.ls_doc_pencil );
                    }
                } else {
                    if( item.item.isComplete() ) {
                        ivNot.setImageResource(R.drawable.ls_check_green);
                    } else {
                        ivNot.setClickable(true);
                        setOfflineImage( ivNot, SG.isOnline, R.drawable.ls_check_grey );
                    }
                }   
            } else {
                ivNot.setVisibility( View.GONE );
            }
        } else {
            switch( item.itemType ) {
            case WrapPO.HEADER_EMPTY:
                rowView = inflater.inflate( R.layout.row_header_empty, parent, false);
                break;
            case WrapPO.HEADER_MORE:
                rowView = inflater.inflate( R.layout.row_header_more, parent, false);
                break;
            default:
                rowView = inflater.inflate( R.layout.row_header, parent, false);
            }
            TextView tvLabel = (TextView) rowView.findViewById(R.id.tvHeader);
            tvLabel.setText( item.itemHeader );
        }
        rowView.setOnLongClickListener( longClick );
        rowView.setOnClickListener( shortClick );
        return rowView;
    }
    @Override
    public long getItemId(int position) {
        if( getItem( position ).item != null )
            return getItem(position).item.asInteger( PO.pubId );
        else
            return (long) -1;
    }
}

这可能是您需要的更复杂的示例,但它显示了如何根据行的内容为行使用不同的布局,并且其中包含可单击的ImageView,就像您需要的那样你的投票按钮。